testcontainers-extensions

Testcontainers Extensions

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

Testcontainers Extensions with advanced testing capabilities.

Makes testing & asserts with Testcontainers even easier.

Usage

Here is an example of Kafka Extension where KafkaContainer is started in PER_RUN mode with topic reset per method:


@TestcontainersKafka(mode = ContainerMode.PER_RUN,
        topics = @Topics(value = "my-topic-name", reset = Topics.Mode.PER_METHOD))
class ExampleTests {

    @ConnectionKafka
    private KafkaConnection kafkaConnection;

    @Test
    void test() {
        var consumer = kafkaConnection.subscribe("my-topic-name");
        kafkaConnection.send("my-topic-name", Event.ofValue("event1"), Event.ofValue("event2"));
        consumer.assertReceivedAtLeast(2, Duration.ofSeconds(5));
    }
}

Here is an example of Postgres Extension where PostgresContainer is started PER_RUN mode and migrations are applied per method:


@TestcontainersPostgreSQL(mode = ContainerMode.PER_RUN,
        migration = @Migration(
                engine = Migration.Engines.FLYWAY,
                apply = Migration.Mode.PER_METHOD,
                drop = Migration.Mode.PER_METHOD))
class ExampleTests {

    @ConnectionPostgreSQL
    private JdbcConnection postgresConnection;

    @Test
    void test() {
        postgresConnection.execute("INSERT INTO users VALUES(1);");
        var usersFound = postgresConnection.queryMany("SELECT * FROM users;", r -> r.getInt(1));
        assertEquals(1, usersFound.size());
    }
}