Embedded Redis for Spring Boot

前端 未结 4 1707
死守一世寂寞
死守一世寂寞 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:18

    You can use an embedded Redis like https://github.com/kstyrc/embedded-redis

    1. Add the dependency to your pom.xml
    2. Adjust the properties for your integration test to point to your embedded redis, for example :

      spring:
        redis:
          host: localhost
          port: 6379
      
    3. Instanciate the embedded redis server in a component that is defined in your tests only :

      @Component
      public class EmbededRedis {
      
          @Value("${spring.redis.port}")
          private int redisPort;
      
          private RedisServer redisServer;
      
          @PostConstruct
          public void startRedis() throws IOException {
              redisServer = new RedisServer(redisPort);
              redisServer.start();
          }
      
          @PreDestroy
          public void stopRedis() {
              redisServer.stop();
          }
      }
      

提交回复
热议问题