Redis Testcontainers
A Testcontainers module providing RedisContainer, which runs Redis, an in-memory data store.
Dependencies
Add the module to your project.
-
Gradle
-
Maven
dependencies {
testImplementation "io.arconia:arconia-testcontainers-redis"
}
<dependency>
<groupId>io.arconia</groupId>
<artifactId>arconia-testcontainers-redis</artifactId>
<scope>test</scope>
</dependency>
Usage
var redis = new RedisContainer("redis:latest");
redis.start();
Redis accepts connections at getRedisUrl(), or on the port returned by getRedisPort(). The container is ready once Redis accepts connections on that port.
Spring Boot Service Connections
With the Spring Boot Testcontainers support (org.springframework.boot:spring-boot-testcontainers), the application connects to a RedisContainer bean annotated with @ServiceConnection, since Spring Boot names the service connection after the image, redis. For an image with another name, set the name explicitly with @ServiceConnection(name = "redis").
@TestConfiguration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
@Bean
@ServiceConnection
RedisContainer redisContainer() {
return new RedisContainer("redis:latest");
}
}
| In Spring Boot applications, the Redis Dev Service runs this container with no code and connects the application to it. |