欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot單元測試中使用mockito框架mock掉整個RedisTemplate的示例

 更新時間:2018年12月07日 08:31:43   作者:Sam哥哥  
今天小編就為大家分享一篇關(guān)于Spring Boot單元測試中使用mockito框架mock掉整個RedisTemplate的示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

概述

當(dāng)我們使用單元測試來驗證應(yīng)用程序代碼時,如果代碼中需要訪問Redis,那么為了保證單元測試不依賴Redis,需要將整個Redis mock掉。在Spring Boot中結(jié)合mockito很容易做到這一點(diǎn),如下代碼:

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

上面的代碼已經(jīng)mock掉大部分的Redis操作了,網(wǎng)友想mock掉其他操作,自行加上即可。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • CountDownLatch同步工具類使用詳解

    CountDownLatch同步工具類使用詳解

    這篇文章主要為大家詳細(xì)介紹了CountDownLatch的使用說明,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • java多線程開發(fā)ScheduledExecutorService簡化方式

    java多線程開發(fā)ScheduledExecutorService簡化方式

    這篇文章主要為大家介紹了java多線程開發(fā)ScheduledExecutorService的簡化方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java自帶定時任務(wù)ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能

    Java自帶定時任務(wù)ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能

    今天小編就為大家分享一篇關(guān)于Java自帶定時任務(wù)ScheduledThreadPoolExecutor實現(xiàn)定時器和延時加載功能,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 淺析Spring Security登錄驗證流程源碼

    淺析Spring Security登錄驗證流程源碼

    這篇文章主要介紹了Spring Security登錄驗證流程源碼解析,本文結(jié)合源碼講解登錄驗證流程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • 使用CI/CD工具Github Action發(fā)布jar到Maven中央倉庫的詳細(xì)介紹

    使用CI/CD工具Github Action發(fā)布jar到Maven中央倉庫的詳細(xì)介紹

    今天通過對Github Action的簡單使用來介紹了CI/CD的作用,這個技術(shù)體系是項目集成交付的趨勢,也是面試中的一個亮點(diǎn)技能。 而且這種方式可以實現(xiàn)“一次配置,隨時隨地集成部署”,感興趣的朋友一起看看吧
    2021-07-07
  • springboot+vue制作后臺管理系統(tǒng)項目

    springboot+vue制作后臺管理系統(tǒng)項目

    本文詳細(xì)介紹了后臺管理使用springboot+vue制作,以分步驟、圖文的形式詳細(xì)講解,大家有需要的可以參考參考
    2021-08-08
  • mybatis解析xml配置中${xxx}占位符的代碼邏輯

    mybatis解析xml配置中${xxx}占位符的代碼邏輯

    本文主要介紹了mybatis解析xml配置中${xxx}占位符的代碼邏輯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2023-05-05
  • springboot集成測試最小化依賴實踐示例

    springboot集成測試最小化依賴實踐示例

    這篇文章主要為大家介紹了springboot集成測試最小化依賴實踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • druid配置數(shù)據(jù)庫連接使用密文密碼方式

    druid配置數(shù)據(jù)庫連接使用密文密碼方式

    這篇文章主要介紹了druid配置數(shù)據(jù)庫連接使用密文密碼方式,具有很好的參考價值,希望對大家有所幫助,
    2023-12-12
  • SpringBoot日程管理Quartz與定時任務(wù)Task實現(xiàn)詳解

    SpringBoot日程管理Quartz與定時任務(wù)Task實現(xiàn)詳解

    定時任務(wù)是企業(yè)級開發(fā)中必不可少的組成部分,諸如長周期業(yè)務(wù)數(shù)據(jù)的計算,例如年度報表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報告,還有搶購類活動的商品上架,這些都離不開定時任務(wù)。本節(jié)將介紹兩種不同的定時任務(wù)技術(shù)
    2022-09-09

最新評論