Hot questions for Using RabbitMQ in integration testing

Top Java Programmings / RabbitMQ / integration testing

Question:

I am trying to create integration test for a Scala / Java application that connects to a RabbitMQ broker. To achieve this I would like an embedded broker that speaks AMQP that I start and stop before each test. Originally I tried to introduce ActiveMQ as an embedded broker with AMQP however the application uses RabbitMQ so only speaks AMQP version 0.9.3 whereas ActiveMQ requires AMQP version 1.0.

Is there another embedded broker I can use in place of ActiveMQ?


Answer:

A completely in-memory solution. Replace the spring.* properties as required.

<dependency>
  <groupId>org.apache.qpid</groupId>
  <artifactId>qpid-broker</artifactId>
  <version>6.1.1</version>
  <scope>test</scope>
</dependency>
public class EmbeddedBroker {
  public void start() {
    Broker broker = new Broker();
    BrokerOptions brokerOptions = new BrokerOptions();
    brokerOptions.setConfigProperty("qpid.amqp_port", environment.getProperty("spring.rabbitmq.port"));
    brokerOptions.setConfigProperty("qpid.broker.defaultPreferenceStoreAttributes", "{\"type\": \"Noop\"}");
    brokerOptions.setConfigProperty("qpid.vhost", environment.getProperty("spring.rabbitmq.virtual-host"));
    brokerOptions.setConfigurationStoreType("Memory");
    brokerOptions.setStartupLoggedToSystemOut(false);
    broker.startup(brokerOptions);
  }
}

Add initial-config.json as a resource:

{
  "name": "Embedded Test Broker",
  "modelVersion": "6.1",
  "authenticationproviders" : [{
    "name": "password",
    "type": "Plain",
    "secureOnlyMechanisms": [],
    "users": [{"name": "guest", "password": "guest", "type": "managed"}]
  }],
  "ports": [{
    "name": "AMQP",
    "port": "${qpid.amqp_port}",
    "authenticationProvider": "password",
    "protocols": [ "AMQP_0_9_1" ],
    "transports": [ "TCP" ],
    "virtualhostaliases": [{
      "name": "${qpid.vhost}",
      "type": "nameAlias"
    }]
  }],
  "virtualhostnodes" : [{
    "name": "${qpid.vhost}",
    "type": "Memory",
    "virtualHostInitialConfiguration": "{ \"type\": \"Memory\" }"
  }]
}

Question:

I have a class A that publishes event E1. E1 is consumed by class B in the same application that is annotated with @RabbitListener. B does some things and then publishes event E2 that is consumed by C etc etc (forming a process chain).

What I want to do is two things:

  1. I want to test A in an integration test but while doing so I'd like to disable the RabbitListener's so that the entire process that is the result of E1 being published is not executed. I only want to assert that A does what it's supposed to and publishes E1. I have managed to accommodate this by setting spring.rabbitmq.listener.auto-startup=false.
  2. I also want to test B in an integration test by publishing E1 to RabbitMQ so that I can be confident that I've configured B's RabbitListerner correctly. But again I don't want C to be called as a side-effect of E2 being published.

I know I can probably do this using mocks but preferably I'd like to test the real deal and using the actual components (including sending the message to an actual RabbitMQ instance that in my case is running in Docker).

Can I achieve this in a nice way in Spring Boot? Or is it perhaps recommended to use @RabbitListenerTest and indeed use mocks?


Answer:

The @RabbitListener has id property:

/**
 * The unique identifier of the container managing for this endpoint.
 * <p>If none is specified an auto-generated one is provided.
 * @return the {@code id} for the container managing for this endpoint.
 * @see org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry#getListenerContainer(String)
 */
String id() default "";

And that RabbitListenerEndpointRegistry#getListenerContainer(String) returns MessageListenerContainer and there you already can control start()/stop() of individual @RabbitListener handler.

Question:

I'm trying to creating some integration tests and I want to stub out the constructor injected RabbitTemplate. What would be the best way of doing this? Here is an example of the current application service constructor and the amqp variable is used further down to create the messages. I'd like to use different stubs of the RabbitTemplate in order to create message scenarios.

private AccountRepository accountRepository;
private UserRepository userRepository;
private RabbitTemplate amqp;

@Autowired
public IdentityApplicationServiceImpl(UserRepository userRepository, AccountRepository accountRepository, 
        RabbitTemplate aRabbitTemplate) {
    this.userRepository = userRepository;
    this.accountRepository = accountRepository;
    this.amqp = aRabbitTemplate;
}

So, for userRepository, accountRepository, and aRabbitTemplate I want to use specific stubs based on the scenarios. Any tips if RabbitTemplate could be stubbed out would be great.


Answer:

Yes, it can be stubbed.

Just use the interface, RabbitOperations or AmqpTemplate.

A good approach would be to mock it and stub the methods you intend to use.

Question:

How can I tell docker-maven-plugin to wait for the RabbitMQ container to fully start up before running integration tests?

This is the plugin configuration I am using in the pom.xml:

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>start-rabbitmq-container</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <images>
                    <image>
                        <alias>rabbitmq</alias>
                        <name>rabbitmq:3.6.10-management</name>
                        <run>
                            <log>
                                <prefix>RABBITMQ</prefix>
                                <color>cyan</color>
                            </log>
                            <namingStrategy>alias</namingStrategy>
                            <ports>
                                <port>5672:5672</port>
                                <port>15672:15672</port>
                            </ports>
                        </run>
                    </image>
                </images>
            </configuration>
        </execution>
        <execution>
            <id>stop-rabbitmq-container</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

At the moment the ITs start executing while RabbitMQ it's still initialising and failing as the server is not available.


Answer:

"While starting a container is it possible to block the execution until some condition is met"

https://dmp.fabric8.io/#start-wait

You can wait for some log output from RabbitMQ container with log:

Regular expression which is applied against the log output of an container and blocks until the pattern is matched. You can use (?s) in the pattern to switch on multi line matching.