Skip to content

redis-developer/testcontainers-redis

Repository files navigation

Testcontainers Redis

Build Status Download License

Testcontainers Redis is an extension of Testcontainers that supports Redis (Standalone and Cluster), Redis Modules, and Redis Enterprise containers.

Getting Started

Gradle

dependencies {
    testImplementation "com.redis:testcontainers-redis:2.2.2"
}

Maven

<dependency>
    <groupId>com.redis</groupId>
    <artifactId>testcontainers-redis</artifactId>
    <version>2.2.2</version>
    <scope>test</scope>
</dependency>

Usage

Redis Example

@Testcontainers
class RedisExampleTest {

    @Container
    private static RedisContainer container = new RedisContainer(
            RedisContainer.DEFAULT_IMAGE_NAME.withTag(RedisContainer.DEFAULT_TAG));

    @Test
    void testSomethingUsingLettuce() {
        // Retrieve the Redis URI from the container
        String redisURI = container.getRedisURI();
        RedisClient client = RedisClient.create(redisURI);
        try (StatefulRedisConnection<String, String> connection = client.connect()) {
            RedisCommands<String, String> commands = connection.sync();
            Assertions.assertEquals("PONG", commands.ping());
        }
    }
}

Redis Stack Example

@Testcontainers
class RedisStackExampleTest {

    @Container
    private static RedisStackContainer container = new RedisStackContainer(
            RedisStackContainer.DEFAULT_IMAGE_NAME.withTag(RedisStackContainer.DEFAULT_TAG));

    @Test
    void testSomethingUsingLettuceMod() {
        // Retrieve the Redis URI from the container
        String redisURI = container.getRedisURI();
        RedisModulesClient client = RedisModulesClient.create(redisURI);
        try (StatefulRedisModulesConnection<String, String> connection = client.connect()) {
            RedisModulesCommands<String, String> commands = connection.sync();
            commands.ftCreate("myIndex", Field.tag("myField").build());
            IndexInfo indexInfo = RedisModulesUtils.indexInfo(commands.ftInfo("myIndex"));
            Assertions.assertEquals(1, indexInfo.getFields().size());
            Assertions.assertEquals("myField", indexInfo.getFields().get(0).getName());
        }
    }
}