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

redis报错汇总

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

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

具体报错如下:

1.实例化报错

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.连接报错

connect timed out

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


3.权限报错

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

NOAUTH Authentication required.

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

        2.密码错误

ERR invalid password

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

需要注意:

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

    
        
        
        
    

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

    public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
        this(poolConfig, host, port, timeout, password, 0, (String)null);
    }

即,需要配置如下参数:

    
        
        
        
        
        
    
    
        
        
        
        
        
    

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

    
        
        
    

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

    public JedisPool(String host, int port) {
        this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
    }

补充:

bean的xml文件格式:










单元测试类:

@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

   
       
       
       
   

    public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port, int timeout, String password) {
        this(poolConfig, host, port, timeout, password, 0, (String)null);
    }

   
       
       
       
       
       
   

   
       
       
       
       
       
   

   
       
       
   

    public JedisPool(String host, int port) {
        this(new GenericObjectPoolConfig(), host, port, 2000, (String)null, 0, (String)null);
    }
 


       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">





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

你可能感兴趣的:(java,redis,单元测试,报错,jedis,junit)