
Testing reactive Java microservices
Prerequisites:
Learn how to test reactive Java microservices in true-to-production environments using MicroShed Testing.
What you’ll learn
You will learn how to write integration tests for reactive Java microservices and to run the tests in true-to-production environments by using containers with MicroShed Testing. MicroShed Testing tests your containerized application from outside the container so that you are testing the exact same image that runs in production. The reactive application in this guide sends and receives messages between services by using an external message broker, Apache Kafka. Using an external message broker enables asynchronous communications between services so that requests are non-blocking and decoupled from responses. You can learn more about reactive Java services that use an external message broker to manage communications in the Creating reactive Java microservices guide.

True-to-production integration testing with MicroShed Testing
Tests sometimes pass during the development and testing stages of an application’s lifecycle but then fail in production because of differences between your development and production environments. While you can create mock objects and custom setups to minimize differences between environments, it is difficult to mimic a production system for an application that uses an external messaging system. MicroShed Testing addresses this problem by enabling the testing of applications in the same Docker containers that you’ll use in production. As a result, your environment remains the same throughout the application’s lifecycle – from development, through testing, and into production. You can learn more about MicroShed Testing in the Testing a MicroProfile or Jakarta EE application guide.
Additional prerequisites
You need to have Docker installed. For installation instructions, refer to the official Docker documentation. You will build and run the microservices in Docker containers. An installation of Apache Kafka is provided in another Docker container.
Getting started
The fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:
git clone https://github.com/openliberty/guide-reactive-service-testing.git
cd guide-reactive-service-testing
The start
directory contains the starting project that you will build upon.
The finish
directory contains the finished project that you will build.
Before you begin, make sure you have all the necessary prerequisites.
Try what you’ll build
The finish
directory in the root of this guide contains the finished application. Give it a try before you proceed.
To try out the tests, go to the finish
directory and run the following Maven goal to install the models
artifact to the local Maven repository:
cd finish
mvn -pl models install
Run the following command to download or update to the latest Open Liberty Docker image:
docker pull icr.io/appcafe/open-liberty:full-java11-openj9-ubi
Next, navigate to the finish/system
directory and run the following Maven goal to build the system
service and run the integration tests on an Open Liberty server in a container:
cd system
mvn verify
You will see the following output:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 33.001 s - in it.io.openliberty.guides.system.SystemServiceIT
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
--- maven-failsafe-plugin:2.22.2:verify (verify) @ system ---
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 52.817 s
Finished at: 2020-03-13T16:28:55-04:00
------------------------------------------------------------------------
This command might take some time to run the first time because the dependencies and the Docker image for Open Liberty must download. If you run the same command again, it will be faster.
You can also try out the inventory
integration tests by repeating the same commands in the finish/inventory
directory.
Testing with the Kafka consumer client
system/pom.xml
1<?xml version='1.0' encoding='utf-8'?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>io.openliberty.guides</groupId>
8 <artifactId>system</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <packaging>war</packaging>
11
12 <properties>
13 <maven.compiler.source>1.8</maven.compiler.source>
14 <maven.compiler.target>1.8</maven.compiler.target>
15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
17 <!-- Liberty configuration -->
18 <liberty.var.default.http.port>9083</liberty.var.default.http.port>
19 <liberty.var.default.https.port>9446</liberty.var.default.https.port>
20 </properties>
21
22 <dependencies>
23 <!-- Provided dependencies -->
24 <dependency>
25 <groupId>jakarta.platform</groupId>
26 <artifactId>jakarta.jakartaee-api</artifactId>
27 <version>8.0.0</version>
28 <scope>provided</scope>
29 </dependency>
30 <dependency>
31 <groupId>org.eclipse.microprofile</groupId>
32 <artifactId>microprofile</artifactId>
33 <version>3.3</version>
34 <type>pom</type>
35 <scope>provided</scope>
36 </dependency>
37 <dependency>
38 <groupId>org.eclipse.microprofile.reactive.messaging</groupId>
39 <artifactId>microprofile-reactive-messaging-api</artifactId>
40 <version>1.0</version>
41 <scope>provided</scope>
42 </dependency>
43
44 <!-- Required dependencies -->
45 <dependency>
46 <groupId>io.openliberty.guides</groupId>
47 <artifactId>models</artifactId>
48 <version>1.0-SNAPSHOT</version>
49 </dependency>
50 <dependency>
51 <groupId>org.apache.kafka</groupId>
52 <artifactId>kafka-clients</artifactId>
53 <version>2.8.1</version>
54 </dependency>
55 <dependency>
56 <groupId>io.reactivex.rxjava3</groupId>
57 <artifactId>rxjava</artifactId>
58 <version>3.1.2</version>
59 </dependency>
60
61 <!-- For tests -->
62 <!-- tag::dependencies[] -->
63 <dependency>
64 <groupId>org.microshed</groupId>
65 <artifactId>microshed-testing-liberty</artifactId>
66 <version>0.9.1</version>
67 <scope>test</scope>
68 </dependency>
69 <dependency>
70 <groupId>org.testcontainers</groupId>
71 <artifactId>kafka</artifactId>
72 <version>1.16.2</version>
73 <scope>test</scope>
74 </dependency>
75 <!-- end::dependencies[] -->
76 <dependency>
77 <groupId>org.junit.jupiter</groupId>
78 <artifactId>junit-jupiter</artifactId>
79 <version>5.7.0</version>
80 <scope>test</scope>
81 </dependency>
82 </dependencies>
83
84 <build>
85 <finalName>${project.artifactId}</finalName>
86 <plugins>
87 <plugin>
88 <groupId>org.apache.maven.plugins</groupId>
89 <artifactId>maven-war-plugin</artifactId>
90 <version>3.3.2</version>
91 <configuration>
92 <packagingExcludes>pom.xml</packagingExcludes>
93 </configuration>
94 </plugin>
95
96 <!-- Liberty plugin -->
97 <plugin>
98 <groupId>io.openliberty.tools</groupId>
99 <artifactId>liberty-maven-plugin</artifactId>
100 <version>3.7.1</version>
101 </plugin>
102
103 <!-- Plugin to run unit tests -->
104 <plugin>
105 <groupId>org.apache.maven.plugins</groupId>
106 <artifactId>maven-surefire-plugin</artifactId>
107 <version>2.22.2</version>
108 </plugin>
109
110 <!-- Plugin to run integration tests -->
111 <plugin>
112 <groupId>org.apache.maven.plugins</groupId>
113 <artifactId>maven-failsafe-plugin</artifactId>
114 <version>2.22.2</version>
115 <executions>
116 <execution>
117 <id>integration-test</id>
118 <goals>
119 <goal>integration-test</goal>
120 </goals>
121 <configuration>
122 <trimStackTrace>false</trimStackTrace>
123 </configuration>
124 </execution>
125 <execution>
126 <id>verify</id>
127 <goals>
128 <goal>verify</goal>
129 </goals>
130 </execution>
131 </executions>
132 </plugin>
133 </plugins>
134 </build>
135</project>
inventory/pom.xml
1<?xml version='1.0' encoding='utf-8'?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4
5 <groupId>io.openliberty.guides</groupId>
6 <artifactId>inventory</artifactId>
7 <version>1.0-SNAPSHOT</version>
8 <packaging>war</packaging>
9
10 <properties>
11 <maven.compiler.source>1.8</maven.compiler.source>
12 <maven.compiler.target>1.8</maven.compiler.target>
13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15 <!-- Liberty configuration -->
16 <liberty.var.default.http.port>9085</liberty.var.default.http.port>
17 <liberty.var.default.https.port>9448</liberty.var.default.https.port>
18 </properties>
19
20 <dependencies>
21 <!-- Provided dependencies -->
22 <dependency>
23 <groupId>jakarta.platform</groupId>
24 <artifactId>jakarta.jakartaee-api</artifactId>
25 <version>8.0.0</version>
26 <scope>provided</scope>
27 </dependency>
28 <dependency>
29 <groupId>javax.enterprise.concurrent</groupId>
30 <artifactId>javax.enterprise.concurrent-api</artifactId>
31 <version>1.1</version>
32 <scope>provided</scope>
33 </dependency>
34 <dependency>
35 <groupId>org.eclipse.microprofile</groupId>
36 <artifactId>microprofile</artifactId>
37 <version>3.3</version>
38 <type>pom</type>
39 <scope>provided</scope>
40 </dependency>
41 <dependency>
42 <groupId>org.eclipse.microprofile.reactive.messaging</groupId>
43 <artifactId>microprofile-reactive-messaging-api</artifactId>
44 <version>1.0</version>
45 <scope>provided</scope>
46 </dependency>
47
48 <!-- Required dependencies -->
49 <dependency>
50 <groupId>io.openliberty.guides</groupId>
51 <artifactId>models</artifactId>
52 <version>1.0-SNAPSHOT</version>
53 </dependency>
54 <dependency>
55 <groupId>org.apache.kafka</groupId>
56 <artifactId>kafka-clients</artifactId>
57 <version>2.8.1</version>
58 </dependency>
59
60 <!-- For tests -->
61 <!-- tag::dependencies[] -->
62 <dependency>
63 <groupId>org.microshed</groupId>
64 <artifactId>microshed-testing-liberty</artifactId>
65 <version>0.9.1</version>
66 <scope>test</scope>
67 </dependency>
68 <dependency>
69 <groupId>org.testcontainers</groupId>
70 <artifactId>kafka</artifactId>
71 <version>1.16.2</version>
72 <scope>test</scope>
73 </dependency>
74 <!-- end::dependencies[] -->
75 <dependency>
76 <groupId>org.junit.jupiter</groupId>
77 <artifactId>junit-jupiter</artifactId>
78 <version>5.7.0</version>
79 <scope>test</scope>
80 </dependency>
81 </dependencies>
82
83 <build>
84 <finalName>${project.artifactId}</finalName>
85 <plugins>
86 <plugin>
87 <groupId>org.apache.maven.plugins</groupId>
88 <artifactId>maven-war-plugin</artifactId>
89 <version>3.3.2</version>
90 <configuration>
91 <packagingExcludes>pom.xml</packagingExcludes>
92 </configuration>
93 </plugin>
94
95 <!-- Liberty plugin -->
96 <plugin>
97 <groupId>io.openliberty.tools</groupId>
98 <artifactId>liberty-maven-plugin</artifactId>
99 <version>3.7.1</version>
100 </plugin>
101
102 <!-- Plugin to run unit tests -->
103 <plugin>
104 <groupId>org.apache.maven.plugins</groupId>
105 <artifactId>maven-surefire-plugin</artifactId>
106 <version>2.22.2</version>
107 </plugin>
108
109 <!-- Plugin to run integration tests -->
110 <plugin>
111 <groupId>org.apache.maven.plugins</groupId>
112 <artifactId>maven-failsafe-plugin</artifactId>
113 <version>2.22.2</version>
114 <executions>
115 <execution>
116 <goals>
117 <goal>integration-test</goal>
118 <goal>verify</goal>
119 </goals>
120 </execution>
121 </executions>
122 </plugin>
123 </plugins>
124 </build>
125</project>
Navigate to the start
directory to begin.
The example reactive application consists of the system
and inventory
microservices. The system
microservice produces messages to the Kafka message broker, and the inventory
microservice consumes messages from the Kafka message broker. You will write integration tests to see how you can use the Kafka consumer and producer client APIs to test each service. MicroShed Testing and Kafka Testcontainers have already been included as required test dependencies in your Maven pom.xml
files for the system
and inventory
services.
The start
directory contains three directories: the system
service directory, the inventory
service directory, and the models
directory. The models
directory contains the model class that defines the structure of the system load data that is used in the application. Run the following Maven goal to install the packaged models
artifact to the local Maven repository so it can be used later by the system
and inventory
services:
mvn -pl models install
If you don’t have the latest Docker image, pull it by running the following command:
docker pull icr.io/appcafe/open-liberty:full-java11-openj9-ubi
With Open Liberty development mode, known as dev mode, you can use MicroShed Testing to run tests on an already running Open Liberty server. Navigate to the start/system
directory.
When you run Open Liberty in development mode, known as dev mode, the server listens for file changes and automatically recompiles and deploys your updates whenever you save a new change. Run the following goal to start Open Liberty in dev mode:
mvn liberty:dev
After you see the following message, your application server in dev mode is ready:
************************************************************** * Liberty is running in dev mode.
Dev mode holds your command-line session to listen for file changes. Open another command-line session to continue, or open the project in your editor.
Now you can add your test files.
The running system
service searches for a Kafka topic to push its messages to. Because there are not yet any running Kafka services, the system
service throws errors. Later in the guide, you will write and run tests that start a Kafka Testcontainer that can communicate with the system
service. This will resolve the errors that you see now.
Configuring your containers
Create a class to externalize your container configurations.
Create theAppContainerConfig
class.system/src/test/java/it/io/openliberty/guides/system/AppContainerConfig.java
AppContainerConfig.java
The AppContainerConfig
class externalizes test container setup and configuration, so you can use the same application containers across multiple tests.The @Container
annotation denotes an application container that is started up and used in the tests.
Two containers are used for testing the system
service: the system
container, which you built, and the kafka
container, which receives messages from the system
service.
The dependsOn()
method specifies that the system
service container must wait until the kafka
container is ready before it can start.
Testing your containers
Now you can start writing the test that uses the configured containers.
Create theSystemServiceIT
class.system/src/test/java/it/io/openliberty/guides/system/SystemServiceIT.java
SystemServiceIT.java
SystemLoad.java
SystemService.java
The test uses the KafkaConsumer
client API and is configured by using the @KafkaConsumerClient
annotation. The consumer client is configured to consume messages from the system.load
topic in the kafka
container. To learn more about Kafka APIs and how to use them, check out the official Kafka Documentation.
To consume messages from a stream, the messages need to be deserialized from bytes. Kafka has its own default deserializer, but a custom deserializer is provided for you. The deserializer is configured to the consumer’s valueDeserializer
and is implemented in the SystemLoad
class.
The running system
service container produces messages to the systemLoad
Kafka topic, as denoted by the @Outgoing
annotation. The testCpuStatus()
test method polls
a record from Kafka every 3 seconds until the timeout limit. It then verifies
that the record polled matches the expected record.
Running the tests
Because you started Open Liberty in dev mode, you can run the tests by pressing the enter/return
key from the command-line session where you started dev mode.
You will see the following output:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 25.674 s - in it.io.openliberty.guides.system.SystemServiceIT
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
Integration tests finished.
After you are finished running tests, stop the Open Liberty server by typing q
in the command-line session where you ran the server, and then press the enter/return
key.
If you aren’t running in dev mode, you can run the tests by running the following command:
mvn verify
You will see the following output:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 33.001 s - in it.io.openliberty.guides.system.SystemServiceIT
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
--- maven-failsafe-plugin:2.22.2:verify (verify) @ system ---
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 52.817 s
Finished at: 2020-03-13T16:28:55-04:00
------------------------------------------------------------------------
Testing with the Kafka producer client
The inventory
service is tested in the same way as the system
service. The only difference is that the inventory
service consumes messages, which means that tests are written to use the Kafka producer client.
Configuring your containers
Navigate to the start/inventory
directory.
The AppContainerConfig
class is provided, and it is configured in the same way as it was for the system
service. The two containers that are configured for use in the inventory
service integration test are the kafka
and inventory
containers.
Testing your containers
As you did with the system
service, run Open Liberty in dev mode to listen for file changes:
mvn liberty:dev
Now you can create your integrated test.
Create theInventoryServiceIT
class.inventory/src/test/java/it/io/openliberty/guides/inventory/InventoryServiceIT.java
InventoryServiceIT.java
SystemLoad.java
InventoryResource.java
The InventoryServiceIT
class uses the KafkaProducer
client API to produce messages in the test environment for the inventory
service container to consume. The @KafkaProducerClient
annotation configures the producer to use the custom serializer provided in the SystemLoad
class. The @KafkaProducerClient
annotation doesn’t include a topic that the client produces messages to because it has the flexibility to produce messages to any topic. In this example, it is configured to produce messages to the system.load
topic.
The testCpuUsage
test method produces a message to Kafka and then verifies
that the response from the inventory
service matches what is expected.
The @RESTClient
annotation injects a REST client proxy of the InventoryResource
class, which allows HTTP requests to be made to the running application. To learn more about REST clients, check out the Consuming RESTful services with template interfaces guide.
Running the tests
Because you started Open Liberty in dev mode, you can run the tests by pressing the enter/return
key from the command-line session where you started dev mode.
You will see the following output:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 32.564 s - in it.io.openliberty.guides.inventory.InventoryServiceIT
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
Integration tests finished.
After you are finished running tests, stop the Open Liberty server by typing q
in the command-line session where you ran the server, and then press the enter/return
key.
If you aren’t running in dev mode, you can run the tests by running the following command:
mvn verify
You will see the following output:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 42.345 s - in it.io.openliberty.guides.inventory.InventoryServiceIT
Results:
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
--- maven-failsafe-plugin:2.22.2:verify (verify) @ inventory ---
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 48.213 s
Finished at: 2020-03-13T16:43:34-04:00
------------------------------------------------------------------------
Great work! You’re done!
You just tested two reactive Java microservices using MicroShed Testing.
Related Links
Learn more about MicroShed Testing.
Guide Attribution
Testing reactive Java microservices by Open Liberty is licensed under CC BY-ND 4.0
Prerequisites:
Nice work! Where to next?
What did you think of this guide?




Thank you for your feedback!
What could make this guide better?
Raise an issue to share feedback
Create a pull request to contribute to this guide
Need help?
Ask a question on Stack Overflow