当前位置: 首页 > news >正文

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

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

需要注意:

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

    <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的构造参数如下:

    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>

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

    <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地址和端口的构造参数,如下:

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

补充:

bean的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/beanshttp://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 {
}

 

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

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

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 {
}

http://www.lryc.cn/news/147277.html

相关文章:

  • AMEYA360:兆易创新获得ISO 26262 ASIL D流程认证, 汽车功能安全管理体系再上新台阶
  • MySQL binlog的几种日志录入格式以及区别
  • C# 案例题
  • 拒绝摆烂!C语言练习打卡第七天
  • 【动态规划】状态压缩dp
  • Java eight 解读流(Stream)、文件(File)、IO和异常处理的使用方法
  • 胜券汇:行业持续轮动 缺乏主线下关注反转预期的方向
  • java+ssm+mysql农场信息管理系统
  • 【App出海成功案例】 | NetMarvel 帮助广告主ARPU增长45%,ECPM增长50%,付费率涨幅30%
  • CSDN每日一练 |『鬼画符门莲台争夺战』『等差数列』『 路灯亮度』2023-08-31
  • 自编码器AE全方位探析:构建、训练、推理与多平台部署
  • SpringBoot - Google EventBus、AsyncEventBus
  • Tauri打包windows应用配置中文界面
  • 深度丨Serverless + AIGC,一场围绕加速创新的升维布局
  • flask日志
  • 新能源汽车动力总成系统及技术
  • 在 WSL2 中使用 NVIDIA Docker 进行全栈开发和深度学习 TensorFlow pytorch GPU 加速
  • 模拟实现应用层协议
  • SAP-MM-冲销凭证布局变更
  • 事务方法中保证数据只插入一次方案探究
  • 高通开发系列 - 5G网络之QTI守护进程服务介绍
  • Ansible学习笔记3
  • DP读书:鲲鹏处理器 架构与编程(十)鲲鹏软件生态与云服务
  • CSS_IOS适配状态栏和IOS底部安全区域
  • 中央仓库更新失败,IDEA报错repository is non-nexus repo, or does not indexed
  • 设计模式--代理模式
  • 链路聚合原理
  • el-table表尾添加合计行,自动合计,且特殊列自定义计算展示
  • uview ui 1.x ActonSheet项太多,设置滚动(亲测有效)
  • STM32 Cubemx 同名外设中断及回调