RabbitMQ-Mock Documentation

This site hosts the technical documentation for RabbitMQ-Mock.

This project aims to emulate RabbitMQ behavior for test purposes.
RabbitMQ-Mock makes it easy to test against a RabbitMQ broker without having to start one, or using Qpid as a replacement.

Getting started

Maven

Add the following dependency to the pom.xml file

<dependency>
    <groupId>com.github.fridujo</groupId>
    <artifactId>rabbitmq-mock</artifactId>
    <version>{page-component-version}</version>
    <scope>test</scope>
</dependency>

Gradle

Add the following dependency to the build.gradle

repositories {
	mavenCentral()
}

// ...

dependencies {
	// ...
	testImplementation('com.github.fridujo:rabbitmq-mock:{page-component-version}')
	// ...
}

When using ConnectionFactory directly

Replace the use of com.rabbitmq.client.ConnectionFactory by MockConnectionFactory

ConnectionFactory factory = new MockConnectionFactory(); (1)
try (Connection conn = factory.newConnection()) {
    try (Channel channel = conn.createChannel()) {
        GetResponse response = channel.basicGet(queueName, autoAck);
        byte[] body = response.getBody();
        long deliveryTag = response.getEnvelope().getDeliveryTag();

        // Do what you need with the body

        channel.basicAck(deliveryTag, false);
    }
}
1 The only thing that changes: substitute the ConnectionFactory with the mock one.

When using Spring

Change underlying RabbitMQ ConnectionFactory by MockConnectionFactory

@Configuration
@Import(AppConfiguration.class) (2)
class TestConfiguration { (1)
    @Bean ConnectionFactory connectionFactory() { (3)
        return new CachingConnectionFactory(new MockConnectionFactory());
    }
}
1 Define or reuse a Spring configuration dedicated for integration tests
2 Import the configuration (maybe partial) containing RabbitMq configuration
3 Override the connectionFactory bean with the Spring wrapper delegating to MockConnectionFactory

Plugins

Some plugins, like the rabbitmq-sharding one, adds more exchange types (ex: "x-modulus-hash").
RabbitMQ-Mock supports default exchange types: direct, fanout, topic and headers.

You can enable the built-in plugin consistent-hash-exchange:

var connectionFactory = new MockConnectionFactory().enableConsistentHashPlugin();

To add a new type of exchange, use the withAdditionalExchange method on a MockConnectionFactory. For example:

var connectionFactory = new MockConnectionFactory().withAdditionalExchange(new FixDelayExchangeCreator());