Using the Embedded Open Liberty JMS Engine for Testing Jakarta EE Applications

Welcome to this in-depth guide on leveraging the embedded Open Liberty JMS engine for testing Jakarta EE 10 applications. Building on our previous tutorials, including the Jakarta EE Tutorial with Examples (#) and Advanced Jakarta EE Unit Testing (#), explores using the embedded Java Message Service (JMS) engine in Open Liberty to test message-driven applications.

We’ll extend the original project with a JMS-based messaging system, configure the embedded JMS engine, and enhance the Message-Driven Bean (MDB) to write messages to a text file. We’ll then verify the file’s contents in an integration test using JUnit 5.

Whether you’re a Java developer, enterprise architect, or DevOps professional, this guide will empower you to test JMS-based applications effectively in cloud-native Java environments. Let’s dive in!

Why Use the Embedded Open Liberty JMS Engine for Testing?

The Jakarta Messaging (JMS) API enables asynchronous communication in Jakarta EE 10 applications, ideal for microservices and event-driven systems. Open Liberty’s embedded JMS engine, provided by the wasJmsServer-1.0 feature, offers a lightweight messaging server for testing without an external broker like IBM MQ. This simplifies Jakarta EE testing and ensures reliability in enterprise Java development.

Benefits of the Embedded JMS Engine

The embedded JMS engine:

  • Runs within Open Liberty, eliminating external dependencies.
  • Supports queues and topics for flexible messaging.
  • Enables fast, repeatable tests, crucial for cloud-native Java.
  • Integrates with Jakarta EE components like MDBs and JAX-RS endpoints.

When to Use It

Use the embedded JMS engine for:

  • Testing message production and consumption in REST APIs.
  • Validating MDBs that process asynchronous messages.
  • Simulating messaging workflows in a controlled environment.

Setting Up the Embedded JMS Engine in Your Jakarta EE Project

Let’s extend the Maven project from the original tutorial, adding JMS support and configuring the embedded JMS engine. We’ll assume you have the project set up in IntelliJ IDEA with UserResource and UserService.

Configuring Maven Dependencies

Update pom.xml to include JMS dependencies, JerseyTest, Mockito, and the Liberty Maven Plugin:

xml

Configuring the Embedded JMS Engine

Update src/main/liberty/config/server.xml to enable the JMS engine and define a queue:

xml

This configuration enables the embedded JMS engine, defines UserQueue, and sets up JNDI resources for the queue and connection factory.

Affiliate Product Recommendation:

  • IntelliJ IDEA Ultimate – Advanced support for JMS and Jakarta EE testing. Buy Now (#).

Implementing a JMS-Based Application

We’ll extend the project by adding a JAX-RS endpoint to send JMS messages and an MDB that writes messages to a text file.

Sending JMS Messages via JAX-RS

Update UserResource in src/main/java/com/example/UserResource.java to send a user creation event to the JMS queue:

java

The addUser endpoint sends a text message to UserQueue with offloading the user creation event.

Consuming JMS Messages and Writing to a Text File

Create UserMessageConsumer in src/main/java/com/example/UserMessageConsumer.java to process messages and write them to a text file:

java

The MDB appends each message to target/messages.txt, creating the file if it doesn’t exist.

Testing the JMS Application

We’ll write tests to validate message production and consumption, using JerseyTest for the JAX-RS endpoint and an integration test to read the text file’s contents.

Unit Testing the JAX-RS Endpoint with JerseyTest

Create a test in src/test/java/com/example/UserResourceJmsJerseyTest.java:

java

This test mocks JMS dependencies with Mockito and verifies that the addUser endpoint sends the correct message.

Integration Testing with the Embedded JMS Engine

Create an integration test in src/test/java/com/example/UserMessageConsumerIT.java to send a message and verify the text file’s contents:

java

This test:

  • Clears messages.txt before each run to ensure isolation.
  • Sends a test message to UserQueue.
  • Waits briefly for the MDB to process the message.
  • Reads messages.txt and verifies it contains the expected message.

Run the test with mvn verify to start Open Liberty (via the Liberty Maven Plugin) and execute the integration test. The test confirms that the MDB correctly writes messages to the file.

Affiliate Product Recommendation:

  • Pluralsight Testing Course – Master JMS and integration testing for Jakarta EE. Enroll Now (#).

Best Practices for JMS Testing with Open Liberty

Adopt these practices to ensure effective Open Liberty JMS testing.

Mock JMS Dependencies for Unit Tests

Mock ConnectionFactory, JMSContext, and MessageProducer with Mockito in unit tests to keep them fast and isolated, as shown in UserResourceJmsJerseyTest. Avoid interacting with the JMS engine for unit tests.

Use the Embedded Engine for Integration Tests

Leverage the embedded JMS engine for integration tests to simulate real messaging scenarios. Configure minimal queues in server.xml to optimize test performance, as demonstrated in UserMessageConsumerIT.

Monitor Test Coverage

Add JaCoCo to pom.xml for test coverage:

xml

Run mvn test to generate reports in target/site/jacoco. Aim for high coverage to ensure robust Jakarta EE testing.

Affiliate Product Recommendation:

  • New Relic Monitoring – Gain insights into application quality post-testing. Try Free (#).

Conclusion

This guide on the embedded Open Liberty JMS engine for testing has shown you how to build and test JMS-based Jakarta EE 10 applications. By extending the original project with a JMS-enabled JAX-RS endpoint and an MDB that writes to a text file, and verifying the output in an integration test, you’ve mastered Open Liberty JMS testing. The embedded JMS engine simplifies testing, making it ideal for cloud-native Java projects. Apply these techniques, explore the affiliate tools, and look out for future tutorials on microservices or advanced JMS configurations to deepen your enterprise Java development expertise!

We also have a Java 21 REST API step-by-step tutorial , that shows you beginner friendly Java REST API integration testing with Podman, with a complete end-to-end development workflow.

Leave a Comment