Spring Boot单元测试中使用mockito框架mock掉整个RedisTemplate的示例

 更新时间:2018年12月07日 08:31:43   作者:Sam哥哥  
今天小编就为大家分享一篇关于Spring Boot单元测试中使用mockito框架mock掉整个RedisTemplate的示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

概述

当我们使用单元测试来验证应用程序代码时,如果代码中需要访问Redis,那么为了保证单元测试不依赖Redis,需要将整个Redis mock掉。在Spring Boot中结合mockito很容易做到这一点,如下代码:

import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.ActiveProfiles;
import static org.mockito.Mockito.when;
/**
 * mock掉整个RedisTemplate
 */
@ActiveProfiles("uttest")
@Configuration
public class RedisTemplateMocker {
  @Bean
  public RedisTemplate redisTemplate() {
    RedisTemplate redisTemplate = Mockito.mock(RedisTemplate.class);
    ValueOperations valueOperations = Mockito.mock(ValueOperations.class);
    SetOperations setOperations = Mockito.mock(SetOperations.class);
    HashOperations hashOperations = redisTemplate.opsForHash();
    ListOperations listOperations = redisTemplate.opsForList();
    ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    when(redisTemplate.opsForSet()).thenReturn(setOperations);
    when(redisTemplate.opsForValue()).thenReturn(valueOperations);
    when(redisTemplate.opsForHash()).thenReturn(hashOperations);
    when(redisTemplate.opsForList()).thenReturn(listOperations);
    when(redisTemplate.opsForZSet()).thenReturn(zSetOperations);
    RedisOperations redisOperations = Mockito.mock(RedisOperations.class);
    RedisConnection redisConnection = Mockito.mock(RedisConnection.class);
    RedisConnectionFactory redisConnectionFactory = Mockito.mock(RedisConnectionFactory.class);
    when(redisTemplate.getConnectionFactory()).thenReturn(redisConnectionFactory);
    when(valueOperations.getOperations()).thenReturn(redisOperations);
    when(redisTemplate.getConnectionFactory().getConnection()).thenReturn(redisConnection);
    return redisTemplate;
  }
}

上面的代码已经mock掉大部分的Redis操作了,网友想mock掉其他操作,自行加上即可。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • springBoot @Scheduled实现多个任务同时开始执行

    springBoot @Scheduled实现多个任务同时开始执行

    这篇文章主要介绍了springBoot @Scheduled实现多个任务同时开始执行,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • Java获取UTC时间的方法详解

    Java获取UTC时间的方法详解

    这篇文章主要介绍了Java获取UTC时间的方法,结合具体实例形式详细分析了Java针对时区、本地时间、时间偏移量等相关操作技巧,需要的朋友可以参考下
    2017-04-04
  • stream中使用peek一些陷阱避免及解决方法

    stream中使用peek一些陷阱避免及解决方法

    这篇文章主要为大家介绍了stream中使用peek一些陷阱避免及解决方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • spring注解之@Valid和@Validated的区分总结

    spring注解之@Valid和@Validated的区分总结

    @Validated和@Valid在基本验证功能上没有太多区别,但在分组、注解地方、嵌套验证等功能上有所不同,下面这篇文章主要给大家介绍了关于spring注解之@Valid和@Validated区分的相关资料,需要的朋友可以参考下
    2022-03-03
  • Java实战之小米交易商城系统的实现

    Java实战之小米交易商城系统的实现

    这篇文章将利用Java实现小米交易商城系统,文中采用的技术有:JSP 、Spring、SpringMVC、MyBatis等,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-04-04
  • 彻底搞懂Java多线程(一)

    彻底搞懂Java多线程(一)

    这篇文章主要给大家介绍了关于Java面试题之多线程和高并发的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2021-07-07
  • JavaWeb中的filter过滤敏感词汇案例详解

    JavaWeb中的filter过滤敏感词汇案例详解

    敏感词、文字过滤是一个网站必不可少的功能,本篇文章主要介绍了JavaWeb中的filter过滤敏感词汇案例,具有一定的参考价值,有需要的可以了解一下,
    2016-11-11
  • VScode+Java配置与使用的详细步骤

    VScode+Java配置与使用的详细步骤

    这篇文章主要介绍了VS code+Java配置与使用的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • Mybatis MappedStatement类核心原理详解

    Mybatis MappedStatement类核心原理详解

    这篇文章主要介绍了Mybatis MappedStatement类,mybatis的mapper文件最终会被解析器,解析成MappedStatement,其中insert|update|delete|select每一个标签分别对应一个MappedStatement
    2022-11-11
  • 新手初学Java数组

    新手初学Java数组

    数组是相同类型数据的有序集合数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们数组的声明创建
    2021-07-07

最新评论