嵌入式Redis用于弹簧启动

我在我的机器上使用我的本地redis-server帮助运行我的集成测试案例,但我想要一个不依赖于任何服务器的嵌入式redis服务器,并且可以在内存数据库中的h2等任何环境中运行。

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @IntegrationTest("server.port:0") @SpringApplicationConfiguration(classes = Application.class) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class MasterIntegrationTest{ } 

您可以使用嵌入式Redis,如https://github.com/kstyrc/embedded-redis

  1. 将依赖项添加到pom.xml
  2. 调整集成测试的属性以指向嵌入式redis,例如:

     spring: redis: host: localhost port: 6379 
  3. 将嵌入式redis服务器安装在仅在测试中定义的组件中:

     @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(); } } 

您可以使用ozimov / embedded-redis作为Maven(-test) -dependency (这是kstyrc / embedded-redis的后继者)。

  1. 将依赖项添加到pom.xml

      ...  it.ozimov embedded-redis 0.7.1 test  
  2. 调整集成测试的应用程序属性

     spring.redis.host=localhost spring.redis.port=6379 
  3. 在测试配置中使用嵌入式redis服务器

     @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(); } } 

另一种巧妙的方法是使用testcontainers库,它可以运行Docker容器中的任何类型的应用程序,Redis也不例外。 我最喜欢的是它与Spring Test生态系统轻微结合。

maven的依赖:

  org.testcontainers testcontainers ${testcontainers.version}  

简单集成测试:

 @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"management.port=0"}) @ContextConfiguration(initializers = AbstractIntegrationTest.Initializer.class) @DirtiesContext public abstract class AbstractIntegrationTest { private static int REDIS_PORT = 6379; @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(REDIS_PORT); public static class Initializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext ctx) { // Spring Boot 1.5.x TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx, "spring.redis.host=" + redis.getContainerIpAddress(), "spring.redis.port=" + redis.getMappedPort(REDIS_PORT)); // Spring Boot 2.x. TestPropertyValues.of( "spring.redis.host:" + redis.getContainerIpAddress(), "spring.redis.port:" + redis.getMappedPort(REDIS_PORT)) .applyTo(ctx); } } } 

你可以看到这个存储库: https : //github.com/caryyu/spring-embedded-redis-server ,与Spring和Spring Boot完全集成

maven依赖

  com.github.caryyu spring-embedded-redis-server 1.1  

spring boot注释

 @Bean public RedisServerConfiguration redisServerConfiguration() { return new RedisServerConfiguration(); } 

application.yml的用法

 spring: redis: port: 6379 embedded: true