Embedded Redis for Spring Boot

前端 未结 4 1702
死守一世寂寞
死守一世寂寞 2021-01-30 05:27

I run my Integration Test cases with Spring Boot with the help of my local Redis server on my machine.

But I want an embedded Redis server which is not dependent on any

4条回答
  •  伪装坚强ぢ
    2021-01-30 06:04

    You can use ozimov/embedded-redis as a Maven(-test)-dependency (this is the successor of kstyrc/embedded-redis).

    1. Add the dependency to your pom.xml

      
        ...
        
          it.ozimov
          embedded-redis
          0.7.1
          test
        
      
    2. Adjust your application properties for your integration test

      spring.redis.host=localhost
      spring.redis.port=6379
      
    3. Use the embedded redis server in a test configuration

      @TestConfiguration
      public static class EmbededRedisTestConfiguration {
      
        private final redis.embedded.RedisServer redisServer;
      
        public EmbededRedisTestConfiguration(@Value("${spring.redis.port}") final int redisPort) throws IOException {
          this.redisServer = new redis.embedded.RedisServer(redisPort);
        }
      
        @PostConstruct
        public void startRedis() {
          this.redisServer.start();
        }
      
        @PreDestroy
        public void stopRedis() {
          this.redisServer.stop();
        }
      }
      

提交回复
热议问题