当前位置 主页 > 服务器问题 > Linux/apache问题 > 最大化 缩小

    详解spring cloud如何使用spring-test进行单元测试(2)

    栏目:Linux/apache问题 时间:2019-11-12 10:29

    备注:如果没有该类,spring-test启动将报错,见下图


    3、新建操作redis的配置类

    package sc.test.config;
    
    import java.io.Serializable;
    
    import org.springframework.boot.autoconfigure.AutoConfigureAfter;
    import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    
    @Configuration
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    public class RedisCacheAutoConfiguration {
    
      @Bean
      public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        //键的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        //值的序列化方式
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
      }
    }
    

    4、新建配置文件application.yml

    server:
     port: 9005
    
    spring:
     application:
      name: sc-redis
     redis:
      host: 127.0.0.1
      password: 
      port: 6379
      timeout: 10000 # 连接超时时间(毫秒)
      database: 0 # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
      lettuce:
       pool:
        max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8
        max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
        max-idle: 8 # 连接池中的最大空闲连接 默认 8
        min-idle: 0 # 连接池中的最小空闲连接 默认 0
    

    5、新建测试类TestRedis.java

    package sc.test.unit;
    
    import java.io.Serializable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.stream.IntStream;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import sc.test.model.User;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class TestRedis {
    
      private static final Logger log = LoggerFactory.getLogger(TestRedis.class);
    
      @Autowired
      private StringRedisTemplate stringRedisTemplate;
    
      @Autowired
      private RedisTemplate<String, Serializable> redisCacheTemplate;
    
      @Test
      public void get() {
        // 测试线程安全
    //    ExecutorService executorService = Executors.newFixedThreadPool(1000);
    //    IntStream.range(0, 1000).forEach(i ->
    //        executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))
    //    );
        stringRedisTemplate.opsForValue().set("key", "{'name':'huangjinjin', 'age':30}");
        final String value = stringRedisTemplate.opsForValue().get("key");
        log.info("[字符缓存结果] - [{}]", value);
        String key = "manage:user:1";
        User u = new User();
        u.setId(1L);
        u.setAge(30);
        u.setPosition("cto");
        u.setUserName("good boy");
        redisCacheTemplate.opsForValue().set(key, u);
        //从缓存获取User对象
        final User user = (User) redisCacheTemplate.opsForValue().get(key);
        log.info("[对象缓存结果] - userName={}, age={}, position={}", //
            user.getUserName(), user.getAge(), user.getPosition());
      }
    }
    
    
    下一篇:没有了