Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Testing Spring Boot JMS with ActiveMQ Artemis and Testcontainers

  • December 12, 2023
  • 5779 Unique Views
  • < 1 min read
Table of Contents
Testcontainers to the RescueWriting the TestConclusion

Currently, I'm teaching JMS with Spring Boot at the University of Applied Science in Bern, Switzerland. We use Apache ActiveMQ Artemis as the JMS message broker. But how can we test our Spring Boot application?

Testcontainers to the Rescue

Currently, there is no Testcontainers Java module for ActiveMQ Artemis. As you can see in the Testcontainers GitHub repository, there is an active activemq branch that may be released soon. But in the meantime, we need another solution.

There is GenericContainer that can be used for any container image:

@Container
static GenericContainer<?> artemis = new GenericContainer<>(
              DockerImageName.parse("apache/activemq-artemis:latest-alpine"))
        .withEnv("ANONYMOUS_LOGIN", "true")
        .withExposedPorts(61616);

As you can see, we need some configuration. First, we use the official ActiveMQ Artemis image, which is available in Docker Hub. Then, we set ANONYMOUS_LOGIN to true. Otherwise, we must provide username and password, which is not needed just for testing. And finally, we must expose the default port to which we want to send our message.

Now, the Artemis JMS client needs to know the URL for the connection. We can use @DynamicPropertySource for that purpose and use the information from the Testcontainers container:

@DynamicPropertySource
static void artemisProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.artemis.broker-url", 
        () -> "tcp://%s:%d".formatted(
              artemis.getHost(), artemis.getMappedPort(61616)));
}

Writing the Test

Finally, everything is prepared, and we can write a test that sends and receives a message:

@Autowired
private JmsTemplate jmsTemplate;

@Test
void sendMessage() throws JMSException {
    jmsTemplate.convertAndSend("testQueue", "Hello, JMS!");

    Message message = jmsTemplate.receive("testQueue");

    assertThat(message).isInstanceOf(TextMessage.class);
    TextMessage textMessage = (TextMessage) message;
    assertThat(textMessage.getText()).isEqualTo("Hello, JMS");
 }

Conclusion

Testcontainers is a fantastic way to start resources as containers. Even if there is no pre-made container, you can always use GenericContainer to run virtually any container image.

Try it out yourself! The source code of the sample project is available on GitHub: https://github.com/simasch/spring-boot-artemis-testcontainers

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (1)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Everton Gonçalves avatar

Everton Gonçalves

9 months ago

very useful! thanks for the content :)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard