Gur*_*der 29 java redis spring-boot
我在我的机器上使用我的本地redis-server帮助运行我的Integration Test案例和Spring启动.
但我想要一个嵌入式Redis服务器,它不依赖于任何服务器,可以在任何环境中运行,比如H2内存数据库.我该怎么做?
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
@SpringApplicationConfiguration(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class MasterIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
Séb*_*mer 44
您可以使用嵌入式Redis,如https://github.com/kstyrc/embedded-redis
调整集成测试的属性以指向嵌入式redis,例如:
spring:
redis:
host: localhost
port: 6379
Run Code Online (Sandbox Code Playgroud)将嵌入式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();
}
}
Run Code Online (Sandbox Code Playgroud)Mar*_*lte 21
您可以使用ozimov/embedded-redis作为Maven(-test)-dependency(这是kstyrc/embedded-redis的后继者).
将依赖项添加到pom.xml
<dependencies>
...
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)调整集成测试的应用程序属性
spring.redis.host=localhost
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)在测试配置中使用嵌入式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();
}
}
Run Code Online (Sandbox Code Playgroud)另一种简洁的方法是使用testcontainers可以运行Docker容器中可以运行的任何类型的应用程序的库,Redis也不例外。我最喜欢的是它与Spring Test生态系统紧密结合。
Maven的依赖:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
简单集成测试:
@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<ConfigurableApplicationContext> {
@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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36441 次 |
| 最近记录: |