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

这篇具有很好参考价值的文章主要介绍了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(访问需要密码(redis服务端设置了密码))

NOAUTH Authentication required.

 出现此错误,说明jedis客户端配置文件没有配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/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 {
}

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

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

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 {
}文章来源地址https://www.toymoban.com/news/detail-676313.html

到了这里,关于redis jedis 单元测试 报错集锦 汇总 junit的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决Gradle在IDEA中运行Java17的Junit单元测试程序报错:module java.base does not “opens java.lang“ to unnamed module

    gradle在IDEA中使用了JDK17运行springboot3.x等程序的时候使用了反射或ASM等会报错:module java.base does not “opens java.lang” to unnamed module,可以通过在IDEA中设置JVM参数解决此问题: 对于单元测试程序,设置了此参数无效,只能通过修改gradle.build脚本解决此问题,在gradle.build中添加如

    2024年02月16日
    浏览(45)
  • Day14:单元测试、Junit单元测试框架、反射、注解

    针对最小的功能单元(方法)进行正确性测试 编写正规的单元测试框架 传统的无法执行自动化测试,且无法得到测试报告 Junit的作用: 测试类取名:原类名+Test(大驼峰) 测试方法取名:test+原函数名称(小驼峰) 测试方法:必须public,无参,无返回值 测试方法上面必须加

    2024年04月14日
    浏览(49)
  • 软件测试实验:Junit单元测试

    目录 前言 实验目的 实验内容 实验要求 实验过程 题目一 题目一测试结果 题目二 题目二实验结果 总结 软件测试是软件开发过程中不可缺少的一个环节,它可以保证软件的质量和功能,发现并修复软件的缺陷和错误。软件测试分为多种类型,其中一种是单元测试,即对软件

    2024年02月02日
    浏览(38)
  • 单元测试 —— JUnit 5 参数化测试

    目录 设置 我们的第一个参数化测试 参数来源 @ValueSource @NullSource @EmptySource @MethodSource @CsvSource @CsvFileSource @EnumSource @ArgumentsSource 参数转换 参数聚合 奖励 总结 如果您正在阅读这篇文章,说明您已经熟悉了JUnit。让我为您概括一下JUnit——在软件开发中,我们开发人员编写的代

    2024年02月03日
    浏览(35)
  • 【单元测试】Junit 4(三)--Junit4断言

    ​ **断言(assertion)**是一种在程序中的一阶逻辑(如:一个结果为真或假的逻辑判断式),目的为了表示与验证软件开发者预期的结果——当程序执行到断言的位置时,对应的断言应该为真。若断言不为真时,程序会中止执行,并给出错误信息。 这里我们直接上例子 接下来我们

    2024年02月08日
    浏览(42)
  • 单元测试junit+mock

    单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。至于“单元”的大小或范围,并没有一个明确的标准,“单元”可以是一个方法、类、功能模块或者子系统。 单元测试通常和白盒测试联系到一起 ,如果单从概念上来讲两者是有区别的,不过我们通

    2024年02月08日
    浏览(52)
  • 【单元测试Junit】

    1.什么是单元测试 ● 单元测试是针对最小的功能单元编写的测试代码 ● Java程序最小的功能单元是方式 ● 单元测试就是针对单个Java方法的测试 2.为什么要使用单元测试 1.使用main()方法测试的缺点 ● 只能有一个main()方法,不能把测试代码分离; ● 没有打印出测试结果和测

    2024年02月06日
    浏览(39)
  • JUnit 5 单元测试框架

    依赖安装 所有支持的注解都在包 org.junit.jupiter.api 下。 基本使用:

    2024年01月20日
    浏览(35)
  • java 单元测试Junit

    所谓 单元测试 ,就是针对最小的功能单元,编写测试代码对其进行正确性测试。为了测试更加方便,有一些第三方的公司或者组织提供了很好用的测试框架,给开发者使用。这里介绍一种Junit测试框架。Junit是第三方公司开源出来的,用于对代码进行单元测试的工具(IDEA已经

    2024年02月07日
    浏览(54)
  • 测开 (Junit 单元测试框架)

    目录 了解 Junit 引入相关依赖 1、Junit注解 @Test @BeforeEach、@BeforeAll @AfterEach @AfterAll 2、断言 1、Assertions - assertEquals 方法 2、Assertions - assertNotEquals 方法 3、Assertions - assertTrue assertFalse方法 4、Assertions - assertNull assertNotNull 小结 3、用例的执行顺序 - 方法排序( @Order 注解) 4、测试套

    2024年02月07日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包