概述
当我们使用单元测试来验证应用程序代码时,如果代码中需要访问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(r