testcontainers-extensions

Testcontainers Extensions RabbitMQ

Minimum required Java version Maven Central

Testcontainers RabbitMQ Extension with advanced testing capabilities.

Features:

Dependency

Gradle

testImplementation "io.goodforgod:testcontainers-extensions-rabbitmq:0.14.0"

Maven

<dependency>
    <groupId>io.goodforgod</groupId>
    <artifactId>testcontainers-extensions-rabbitmq</artifactId>
    <version>0.14.0</version>
    <scope>test</scope>
</dependency>

RabbitMQ Client

RabbitMQ Java client must be on classpath.

Gradle

testImplementation "com.rabbitmq:amqp-client:5.21.0"

Usage

@TestcontainersRabbitMQ(mode = ContainerMode.PER_CLASS,
        topology = @Topology(
                queues = @Queue(name = "orders"),
                exchanges = @Exchange(name = "events", type = Exchange.Type.DIRECT),
                bindings = @Binding(queue = "orders", exchange = "events", routingKey = "orders.created")))
class ExampleTests {

    @ConnectionRabbitMQ
    private RabbitMQConnection connection;

    @Test
    void test() {
        var consumer = connection.subscribe("orders");
        connection.send("events", "orders.created", Event.ofValue("value1"), Event.ofValue("value2"));
        consumer.assertReceivedAtLeast(2, Duration.ofSeconds(5));
    }
}

Connection

RabbitMQConnection can be injected to a field or method parameter and used to publish messages, subscribe queues, assert received messages and declare topology.

Annotation

@TestcontainersRabbitMQ automatically starts RabbitMQ with the specified image in different modes. It also supports declaring topology before tests run.

Available container modes:

Topology

Topology can be configured directly in annotation:

@TestcontainersRabbitMQ(
        topology = @Topology(
                queues = @Queue(name = "orders"),
                exchanges = @Exchange(name = "events", type = Exchange.Type.DIRECT),
                bindings = @Binding(queue = "orders", exchange = "events", routingKey = "orders.created")))
class ExampleTests {}

Topology reset modes:

External Connection

If you want to use an already running RabbitMQ instance, the extension supports:

Prefix conversion rules: cut prefix, lower-case and replace _ with ..

License

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