redis jedis 单元测试 报错集锦 汇总 junit

redis报错汇总

在单元测试时,使用jedis通常遇到如下报错:

实例化报错->连接报错->权限报错。此报错是有顺序的:例如,若连接报错,说明实例化完成,即配置文件配对了。若权限报错,说明连接通了,但密码错误。若实例化就报错,说明配置文件配错了,没法启动redis客户端,更别说去连接了。

具体报错如下:

1.实例化报错

java 复制代码
Failed to load ApplicationContext.
Error creating bean with name 'jedisPool' defined in class path resource [applicationContext-redis.xml]: 
Unsatisfied dependency expressed through constructor parameter 0: 
Ambiguous argument values for parameter of type [org.apache.commons.pool2.impl.GenericObjectPoolConfig] 
- did you specify the correct bean references as arguments?

出现此错误,通常是配置文件出错:配置JedisPool出错。

2.连接报错

java 复制代码
connect timed out

出现此错误,通常是网络问题。一般在公司里,内网外网防火墙等各种网络情况。记得切换网络。

3.权限报错

1.没有配置password(如果需要密码)

java 复制代码
NOAUTH Authentication required.

出现此错误,说明配置文件没有配password。

2.密码错误

java 复制代码
ERR invalid password

出现此错误,说明密码错了。

需要注意:

如下配置是错误的,这也是导致实例化报错的主要原因。

XML 复制代码
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.100.12"/>
        <constructor-arg name="port" value="6379"/>
        <constructor-arg name="password" value="xxx"/>
    </bean>

查看jedis源码,发现设置密码,JedisPool的构造参数如下:

java 复制代码
    public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
        this(poolConfig, host, port, timeout, password, 0, (String)null);
    }

即,需要配置如下参数:

XML 复制代码
    <bean class="redis.clients.jedis.JedisPool" id="jedisPool" >
        <constructor-arg name="host" value="192.168.100.12"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="password" value="xxx"></constructor-arg>
        <constructor-arg name="timeout" value="3000"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>
    <bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">
        <property name="maxIdle" value="300" />
        <property name="maxTotal" value="1000" />
        <property name="maxWaitMillis" value="1000" />
        <property name="testOnBorrow" value="false" />
        <property name="blockWhenExhausted" value="false" />
    </bean>

如果redis没有设置密码的话,配置就可以很简单:

XML 复制代码
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="192.168.100.12"/>
        <constructor-arg name="port" value="6379"/>
    </bean>

因为JedisPool提供了只需要ip地址和端口的构造参数,如下:

java 复制代码
    public JedisPool(String host, int port) {
        this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
    }

补充:

bean的xml文件格式:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 这中间写bean -->
<!--    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">-->
<!--        <constructor-arg name="host" value="192.168.100.12"/>-->
<!--        <constructor-arg name="port" value="6379"/>-->
<!--    </bean>-->

</beans>

单元测试类:

java 复制代码
@RunWith(SpringRunner.class)//spring整合JUnit4
@ContextConfiguration(locations={"classpath:applicationContext-redis.xml"})//加载spring配置文件
public class BaseRedisTest {
}

==================分割线====================

文章到此已经结束,以下是紫薯布丁

Failed to load ApplicationContext.

Error creating bean with name 'jedisPool' defined in class path resource [applicationContext-redis.xml]:

Unsatisfied dependency expressed through constructor parameter 0:

Ambiguous argument values for parameter of type [org.apache.commons.pool2.impl.GenericObjectPoolConfig]

  • did you specify the correct bean references as arguments?

connect timed out

NOAUTH Authentication required.

ERR invalid password

<bean id="jedisPool" class="redis.clients.jedis.JedisPool">

<constructor-arg name="host" value="192.168.100.12"/>

<constructor-arg name="port" value="6379"/>

<constructor-arg name="password" value="xxx"/>

</bean>

public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {

this(poolConfig, host, port, timeout, password, 0, (String)null);

}

<bean class="redis.clients.jedis.JedisPool" id="jedisPool" >

<constructor-arg name="host" value="192.168.100.12"></constructor-arg>

<constructor-arg name="port" value="6379"></constructor-arg>

<constructor-arg name="password" value="xxx"></constructor-arg>

<constructor-arg name="timeout" value="3000"></constructor-arg>

<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>

</bean>

<bean class="redis.clients.jedis.JedisPoolConfig" id="jedisPoolConfig">

<property name="maxIdle" value="300" />

<property name="maxTotal" value="1000" />

<property name="maxWaitMillis" value="1000" />

<property name="testOnBorrow" value="false" />

<property name="blockWhenExhausted" value="false" />

</bean>

<bean id="jedisPool" class="redis.clients.jedis.JedisPool">

<constructor-arg name="host" value="192.168.100.12"/>

<constructor-arg name="port" value="6379"/>

</bean>

public JedisPool(String host, int port) {

this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);

}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 这中间写bean -->

<!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool">-->

<!-- <constructor-arg name="host" value="192.168.100.12"/>-->

<!-- <constructor-arg name="port" value="6379"/>-->

<!-- </bean>-->

</beans>

@RunWith(SpringRunner.class)//spring整合JUnit4

@ContextConfiguration(locations={"classpath:applicationContext-redis.xml"})//加载spring配置文件

public class BaseRedisTest {

}

相关推荐
西红柿维生素4 小时前
5mins了解redis底层数据结&源码
数据库·redis·缓存
猿究院-陆昱泽4 小时前
Redis 五大核心数据结构知识点梳理
redis·后端·中间件
hong_zc7 小时前
redis之缓存
数据库·redis·缓存
测试老哥10 小时前
软件测试之单元测试详解
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
茉莉玫瑰花茶13 小时前
Redis - Bitmap 类型
数据库·redis·缓存
麦兜*20 小时前
Redis 7.0 新特性深度解读:迈向生产级的新纪元
java·数据库·spring boot·redis·spring·spring cloud·缓存
AAA修煤气灶刘哥20 小时前
Redis为什么快??3 个底层逻辑拆明白,性能优化不用瞎折腾
redis·后端·架构
坐吃山猪21 小时前
Redis03-缓存知识点
redis·缓存
ANYOLY1 天前
Redis 面试宝典
数据库·redis·面试
洲覆1 天前
Redis 核心数据类型:从命令、结构到实战应用
服务器·数据库·redis·缓存