激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - Spring Boot單元測試中使用mockito框架mock掉整個RedisTemplate的示例

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

2021-06-19 10:53Sam哥哥 Java教程

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

概述

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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í)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

原文鏈接:https://blog.csdn.net/linsongbin1/article/details/82761965

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费a级毛片永久免费 | 5xsq在线视频| 免费a级黄色片 | 成人福利视频在线 | 国产精品久久久久无码av | 国产毛片毛片 | 九九热播视频 | 一级黄色在线免费观看 | 日韩三区视频 | 久久艹国产精品 | 免费观看黄视频 | 成人一级免费视频 | 麻豆国产网站 | 国产一区二区欧美 | 大逼逼影院 | 欧美日韩成人一区二区 | 99精品视频在线观看免费播放 | 美女喷水网站 | 国产日韩精品欧美一区视频 | 羞羞视频免费观看入口 | 性高跟鞋xxxxhd4kvideos | 国产精品免费成人 | 亚洲va久久久噜噜噜久久男同 | 日本综合久久 | 88xx成人永久免费观看 | 欧产日产国产精品99 | 性欧美极品xxxx欧美一区二区 | 日韩精品一二三 | 国产精品99久久久久久久女警 | 国产一区二区视频观看 | 成人在线视频一区 | 99久久超碰中文字幕伊人 | 毛片免| 91性高湖久久久久久久久网站 | 4p一女两男做爰在线观看 | 国产又白又嫩又紧又爽18p | 久久毛片 | 亚洲三区精品 | 美女一级毛片 | 日本高清无遮挡 | 一级毛片免费在线 |