testcontainers-extensions

Testcontainers Extensions Redis

Minimum required Java version Maven Central GitHub Action Coverage Maintainability Rating Lines of Code

Testcontainers Redis Extension with advanced testing capabilities.

Features:

Dependency :rocket:

Gradle

testImplementation "io.goodforgod:testcontainers-extensions-redis:0.12.1"

Maven

<dependency>
    <groupId>io.goodforgod</groupId>
    <artifactId>testcontainers-extensions-redis</artifactId>
    <version>0.12.1</version>
    <scope>test</scope>
</dependency>

Redis Driver

Redis Jedis Client must be on classpath, if it is somehow not on your classpath already, don’t forget to add:

Gradle

testImplementation "redis.clients:jedis:4.4.3"

Maven

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.4.3</version>
    <scope>test</scope>
</dependency>

Content

Usage

Test with container start in PER_RUN mode will look like:

@TestcontainersRedis(mode = ContainerMode.PER_RUN)
class ExampleTests {

  @ConnectionRedis 
  private RedisConnection connection;
  
  @Test
  void test() {
    connection.commands().set("11", "1");
    connection.commands().set("12", "2");
    assertEquals(2, connection.countPrefix(RedisKey.of("1")));
  }
}

Connection

RedisConnection is an abstraction with asserting data in database container and easily manipulate container connection settings. You can inject connection via @ConnectionRedis as field or method argument or manually create it from container or manual settings.

class ExampleTests {

  private static final RedisContainer container = new RedisContainer();

  @Test
  void test() {
    container.start();
    RedisConnection connection = RedisConnection.forContainer(container);
    connection.commands().set("11", "1");
    connection.commands().set("12", "2");
    assertEquals(2, connection.countPrefix(RedisKey.of("1")));
  }
}

Annotation

@TestcontainersRedis - allow automatically start container with specified image in different modes without the need to configure it.

Available containers modes:

Simple example on how to start container per class, no need to configure container:

@TestcontainersRedis(mode = ContainerMode.PER_CLASS)
class ExampleTests {

    @Test
    void test(@ConnectionRedis RedisConnection connection) {
        assertNotNull(connection);
    }
}

That’s all you need.

It is possible to customize image with annotation image parameter.

Image also can be provided from environment variable:

@TestcontainersRedis(image = "${MY_IMAGE_ENV|redis:7.4-alpine}")
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Image syntax:

Manual Container

When you need to manually configure container with specific options, you can provide such container as instance that will be used by @TestcontainersRedis, this can be done using @ContainerRedis annotation for container.

Example:

@TestcontainersRedis(mode = ContainerMode.PER_CLASS)
class ExampleTests {

    @ContainerRedis
    private static final RedisContainer container = new RedisContainer().withNetworkAliases("myredis");
    
    @Test
    void test(@ConnectionRedis RedisConnection connection) {
        assertEquals("myredis", connection.paramsInNetwork().get().host());
    }
}

Network

In case you want to enable Network.SHARED for containers you can do this using network & shared parameter in annotation:

@TestcontainersRedis(network = @Network(shared = true))
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Default alias will be created by default, even if nothing was specified (depends on implementation).

You can provide also custom alias for container. Alias can be extracted from environment variable also or default value can be provided if environment is missing.

In case specified environment variable is missing default alias will be created:

@TestcontainersRedis(network = @Network(alias = "${MY_ALIAS_ENV|my_default_alias}"))
class ExampleTests {

    @Test
    void test() {
        // test
    }
}

Image syntax:

Annotation Connection

RedisConnection - can be injected to field or method parameter and used to communicate with running container via @ConnectionRedis annotation. RedisConnection provides connection parameters, useful asserts, checks, etc. for easier testing.

Example:

@TestcontainersRedis(mode = ContainerMode.PER_CLASS, image = "redis:7.4-alpine")
class ExampleTests {

    @ConnectionRedis
    private RedisConnection connection;

    @Test
    void test() {
        connection.commands().set("11", "1");
        connection.commands().set("12", "2");
        assertEquals(2, connection.countPrefix(RedisKey.of("1")));
    }
}

External Connection

In case you want to use some external Redis instance that is running in CI or other place for tests (due to docker limitations or other), you can use special environment variables and extension will use them to propagate connection and no Redis containers will be running in such case.

Special environment variables:

License

This project licensed under the Apache License 2.0 - see the LICENSE file for details.