zoukankan      html  css  js  c++  java
  • 单测中mocker redis

    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
    import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
    import org.mockito.Mockito;
    import org.redisson.api.RedissonClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.data.redis.cache.RedisCacheConfiguration;
    import org.springframework.data.redis.cache.RedisCacheManager;
    import org.springframework.data.redis.cache.RedisCacheWriter;
    import org.springframework.data.redis.connection.RedisConnection;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.*;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    import static org.mockito.Mockito.when;
    
    @Configuration
    @Profile("test")
    public class RedisTemplateMocker {
    
        @Bean
        public RedissonClient getRedisson(){
            RedissonClient mock = Mockito.mock(RedissonClient.class);
            return mock;
        }
    
        @Bean
        public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
            RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
            RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                    .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer()));
            return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        }
    
        @Bean
        public RedisTemplate redisTemplate() {
            RedisTemplate redisTemplate = Mockito.mock(RedisTemplate.class);
            redisTemplate.setDefaultSerializer(redisSerializer());
            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;
        }
    
        public RedisSerializer<Object> redisSerializer() {
    
            ObjectMapper objectMapper = new ObjectMapper();
            //反序列化时候遇到不匹配的属性并不抛出异常
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            //序列化时候遇到空对象不抛出异常
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            //反序列化的时候如果是无效子类型,不抛出异常
            objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
            //不使用默认的dateTime进行序列化,
            objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
            //使用JSR310提供的序列化类,里面包含了大量的JDK8时间序列化类
            objectMapper.registerModule(new JavaTimeModule());
            //启用反序列化所需的类型信息,在属性中添加@class
            objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
            //配置null值的序列化器
            GenericJackson2JsonRedisSerializer.registerNullValueSerializer(objectMapper, null);
            return new GenericJackson2JsonRedisSerializer(objectMapper);
        }
    
    }
    

      

  • 相关阅读:
    java jdbc笔记整理
    Spring IOC (构造器注入)
    WEB-INF目录与META-INF目录的作用[转]
    [转]LL(1)文法判别之First集合、Follow集合、Select集合求法
    java读取TXT文件的方法 (转)
    Ubuntu 12.04下搭建Web服务器 (MySQL+PHP+Apache)(转)
    题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
    error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCR...(转)
    汇编leal命令的灵活用法及理解
    C++之继承
  • 原文地址:https://www.cnblogs.com/zhlblogs/p/14805905.html
Copyright © 2011-2022 走看看