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
  • 11340 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

[Unit] Testing Supabase in Kotlin using Test Containers

In this article, I’ll dive into several methods I’ve been looking into to unit test a Kotlin application using Supabase and why I finally decided to go for a Docker Compose / Test Containers solution.

API Mocking: Essential and Redundant

Is API mocking in unit tests important or does it do nothing? The answer is nuanced but we do need such tests to reduce daily churn.

Beyond Pass/Fail- a Modern Approach to Java Integration Testing

Tests can run limited sets of assertions on your code, or reveal important insights about how your application really works!

Lego character that removes block on keyboard
Build and Test Non-Blocking Web Applications with Spring WebFlux, Kotlin and Coroutines

In this article, we will develop a simple RESTful API using Spring WebFlux and aim to leverage the special Kotlin extensions in Spring.

Configuring Spring Boot to Build a Docker Image with Azul Zulu and Debug Options

The Spring Boot Maven Plugin makes creating a Docker image from your app very easy! Let’s see how define the Java runtime used and more.

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

2 years ago

very useful! thanks for the content :)

22

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.

Mastodon

Subscribe to foojay updates:

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