Testing microservices with the Arquillian managed container

duration 15 minutes

Prerequisites:

Learn how to develop tests for your microservices with the Arquillian managed container and run the tests on Open Liberty.

What you’ll learn

You will learn how to develop tests for your microservices by using the Arquillian Liberty Managed container and JUnit with Maven on Open Liberty. Arquillian is a testing framework to develop automated functional, integration and acceptance tests for your Java applications. Arquillian sets up the test environment and handles the application server lifecycle for you so you can focus on writing tests.

You will develop Arquillian tests that use JUnit as the runner and build your tests with Maven using the Liberty Maven plug-in. This technique simplifies the process of managing Arquillian dependencies and the setup of your Arquillian managed container.

You will work with an inventory microservice, which stores information about various systems. The inventory service communicates with the system service on a particular host to retrieve its system properties and store them. You will develop functional and integration tests for the microservices. You will also learn about the Maven and Liberty configurations so that you can run your tests on Open Liberty with the Arquillian Liberty Managed 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-arquillian-managed.git
cd guide-arquillian-managed

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

Run the following commands to navigate to the finish directory and run the tests:

cd finish
mvn clean package
mvn liberty:create liberty:install-feature
mvn liberty:configure-arquillian
mvn failsafe:integration-test

Look for the following output:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running it.io.openliberty.guides.system.SystemArquillianIT
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.133 s - in it.io.openliberty.guides.system.SystemArquillianIT
[INFO] Running it.io.openliberty.guides.inventory.InventoryArquillianIT
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.297 s - in it.io.openliberty.guides.
...
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

Developing Arquillian tests

Navigate to the start directory to begin.

You’ll develop tests that use Arquillian and JUnit to verify the inventory microservice as an endpoint and the functions of the InventoryResource class. The code for the microservices is in the src/main/java/io/openliberty/guides directory.

When you run Open Liberty in dev mode, dev mode 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 Liberty instance is ready in dev mode:

**************************************************************
*    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.

Create the InventoryArquillianIT test class.
src/test/java/it/io/openliberty/guides/inventory/InventoryArquillianIT.java

InventoryArquillianIT.java

  1// tag::copyright[]
  2/*******************************************************************************
  3 * Copyright (c) 2019, 2022 IBM Corporation and others.
  4 * All rights reserved. This program and the accompanying materials
  5 * are made available under the terms of the Eclipse Public License 2.0
  6 * which accompanies this distribution, and is available at
  7 * http://www.eclipse.org/legal/epl-2.0/
  8 *
  9 * SPDX-License-Identifier: EPL-2.0
 10 *******************************************************************************/
 11// end::copyright[]
 12package it.io.openliberty.guides.inventory;
 13
 14import java.net.URL;
 15import java.util.List;
 16
 17import jakarta.inject.Inject;
 18import jakarta.json.JsonObject;
 19import jakarta.ws.rs.client.Client;
 20import jakarta.ws.rs.client.ClientBuilder;
 21import jakarta.ws.rs.client.WebTarget;
 22import jakarta.ws.rs.core.Response;
 23
 24import org.jboss.arquillian.container.test.api.Deployment;
 25import org.jboss.arquillian.container.test.api.RunAsClient;
 26import org.jboss.arquillian.junit.Arquillian;
 27import org.jboss.arquillian.junit.InSequence;
 28import org.jboss.arquillian.test.api.ArquillianResource;
 29import org.jboss.shrinkwrap.api.ShrinkWrap;
 30import org.jboss.shrinkwrap.api.spec.WebArchive;
 31import org.junit.Assert;
 32import org.junit.Test;
 33import org.junit.runner.RunWith;
 34
 35import io.openliberty.guides.inventory.InventoryResource;
 36import io.openliberty.guides.inventory.model.InventoryList;
 37import io.openliberty.guides.inventory.model.SystemData;
 38
 39// tag::RunWith[]
 40@RunWith(Arquillian.class)
 41// end::RunWith[]
 42public class InventoryArquillianIT {
 43
 44    // tag::warName[]
 45    private static final String WARNAME = System.getProperty("arquillian.war.name");
 46    // end::warName[]
 47    private final String INVENTORY_SYSTEMS = "inventory/systems";
 48    private Client client = ClientBuilder.newClient();
 49
 50    // tag::Deployment[]
 51    // tag::Testable[]
 52    @Deployment(testable = true)
 53    // end::Testable[]
 54    public static WebArchive createDeployment() {
 55        // tag::WebArchive[]
 56        WebArchive archive = ShrinkWrap.create(WebArchive.class, WARNAME)
 57        // end::WebArchive[]
 58                                       .addPackages(true, "io.openliberty.guides");
 59        return archive;
 60    }
 61    // end::Deployment[]
 62
 63    // tag::ArquillianResource[]
 64    @ArquillianResource
 65    // end::ArquillianResource[]
 66    private URL baseURL;
 67
 68    // tag::InventoryResource[]
 69    @Inject
 70    InventoryResource invSrv;
 71    // end::InventoryResource[]
 72
 73    @Test
 74    // tag::RunAsClient[]
 75    @RunAsClient
 76    // end::RunAsClient[]
 77    // tag::InSequence1[]
 78    @InSequence(1)
 79    // end::InSequence1[]
 80    // tag::testInventoryEndpoints[]
 81    public void testInventoryEndpoints() throws Exception {
 82        String localhosturl = baseURL + INVENTORY_SYSTEMS + "/localhost";
 83
 84        WebTarget localhosttarget = client.target(localhosturl);
 85        Response localhostresponse = localhosttarget.request().get();
 86
 87        Assert.assertEquals("Incorrect response code from " + localhosturl, 200,
 88                            localhostresponse.getStatus());
 89
 90        JsonObject localhostobj = localhostresponse.readEntity(JsonObject.class);
 91        Assert.assertEquals("The system property for the local and remote JVM "
 92                        + "should match", System.getProperty("os.name"),
 93                            localhostobj.getString("os.name"));
 94
 95        String invsystemsurl = baseURL + INVENTORY_SYSTEMS;
 96
 97        WebTarget invsystemstarget = client.target(invsystemsurl);
 98        Response invsystemsresponse = invsystemstarget.request().get();
 99
100        Assert.assertEquals("Incorrect response code from " + localhosturl, 200,
101                            invsystemsresponse.getStatus());
102
103        JsonObject invsystemsobj = invsystemsresponse.readEntity(JsonObject.class);
104
105        int expected = 1;
106        int actual = invsystemsobj.getInt("total");
107        Assert.assertEquals("The inventory should have one entry for localhost",
108                            expected, actual);
109        localhostresponse.close();
110    }
111    // end::testInventoryEndpoints[]
112
113    @Test
114    // tag::InSequence2[]
115    @InSequence(2)
116    // end::InSequence2[]
117    // tag::testInventoryResourceFunctions[]
118    public void testInventoryResourceFunctions() {
119        // Listing the inventory contents that were stored in the previous test
120        // tag::listContents[]
121        InventoryList invList = invSrv.listContents();
122        // end::listContents[]
123        Assert.assertEquals(1, invList.getTotal());
124
125        List<SystemData> systemDataList = invList.getSystems();
126        Assert.assertTrue(systemDataList.get(0).getHostname().equals("localhost"));
127
128        Assert.assertTrue(systemDataList.get(0).getProperties().get("os.name")
129                                        .equals(System.getProperty("os.name")));
130    }
131    // end::testInventoryResourceFunctions[]
132}

Notice that the JUnit Arquillian runner runs the tests instead of the standard JUnit runner. The @RunWith annotation preceding the class tells JUnit to run the tests by using Arquillian.

The method annotated by @Deployment defines the content of the web archive, which is going to be deployed onto the Open Liberty. The tests are either run on or against the Liberty instance. The testable = true attribute enables the deployment to run the tests "in container", that is the tests are run on the Liberty instance.

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 
  5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6
  7    <modelVersion>4.0.0</modelVersion>
  8
  9    <groupId>io.openliberty.guides</groupId>
 10    <artifactId>guide-arquillian-managed</artifactId>
 11    <version>1.0-SNAPSHOT</version>
 12    <packaging>war</packaging>
 13
 14    <properties>
 15        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 16        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 17        <maven.compiler.source>11</maven.compiler.source>
 18        <maven.compiler.target>11</maven.compiler.target>
 19        <!-- Liberty configuration -->
 20        <liberty.var.http.port>9080</liberty.var.http.port>
 21        <liberty.var.https.port>9443</liberty.var.https.port>
 22        <!-- arquillian configuration -->
 23        <!-- tag::ArquillianWarName[] -->
 24        <arquillian.war.name>arquillian-managed</arquillian.war.name>
 25        <!-- end::ArquillianWarName[] -->
 26    </properties>
 27
 28    <dependencyManagement>
 29        <dependencies>
 30            <!-- tag::arquillian-bom[] -->
 31            <dependency>
 32                <groupId>org.jboss.arquillian</groupId>
 33                <artifactId>arquillian-bom</artifactId>
 34                <version>1.8.0.Final</version>
 35                <type>pom</type>
 36                <scope>import</scope>
 37            </dependency>
 38            <!-- end::arquillian-bom[] -->
 39        </dependencies>
 40    </dependencyManagement>
 41
 42    <dependencies>
 43        <!-- Provided dependencies -->
 44        <dependency>
 45            <groupId>jakarta.platform</groupId>
 46            <artifactId>jakarta.jakartaee-api</artifactId>
 47            <version>10.0.0</version>
 48            <scope>provided</scope>
 49        </dependency>
 50        <dependency>
 51            <groupId>org.eclipse.microprofile</groupId>
 52            <artifactId>microprofile</artifactId>
 53            <version>6.1</version>
 54            <type>pom</type>
 55            <scope>provided</scope>
 56        </dependency>
 57        <!-- For tests -->
 58        <dependency>
 59            <groupId>junit</groupId>
 60            <artifactId>junit</artifactId>
 61            <version>4.13.2</version>
 62            <scope>test</scope>
 63        </dependency>
 64        <dependency>
 65            <groupId>org.jboss.resteasy</groupId>
 66            <artifactId>resteasy-client</artifactId>
 67            <version>6.2.7.Final</version>
 68            <scope>test</scope>
 69        </dependency>
 70        <dependency>
 71            <groupId>org.jboss.resteasy</groupId>
 72            <artifactId>resteasy-json-binding-provider</artifactId>
 73            <version>6.2.7.Final</version>
 74            <scope>test</scope>
 75        </dependency>
 76        <dependency>
 77            <groupId>org.glassfish</groupId>
 78            <artifactId>jakarta.json</artifactId>
 79            <version>2.0.1</version>
 80            <scope>test</scope>
 81        </dependency>
 82        <!-- For JDK 9 & above Support -->
 83        <dependency>
 84            <groupId>javax.xml.bind</groupId>
 85            <artifactId>jaxb-api</artifactId>
 86            <version>2.3.1</version>
 87        </dependency>
 88        <dependency>
 89            <groupId>javax.activation</groupId>
 90            <artifactId>activation</artifactId>
 91            <version>1.1.1</version>
 92        </dependency>
 93        <!-- tag::arquillian-liberty-managed-junit[] -->
 94        <dependency>
 95            <groupId>io.openliberty.arquillian</groupId>
 96            <artifactId>arquillian-liberty-managed-jakarta-junit</artifactId>
 97            <version>2.1.3</version>
 98            <type>pom</type>
 99            <scope>test</scope>
100        </dependency>
101        <!-- end::arquillian-liberty-managed-junit[] -->
102        <!-- tag::shrinkwrap-api[] -->
103        <dependency>
104            <groupId>org.jboss.shrinkwrap</groupId>
105            <artifactId>shrinkwrap-api</artifactId>
106            <scope>test</scope>
107        </dependency>
108        <!-- end::shrinkwrap-api[] -->
109    </dependencies>
110
111    <build>
112        <finalName>${project.artifactId}</finalName>
113        <plugins>
114            <plugin>
115                <groupId>org.apache.maven.plugins</groupId>
116                <artifactId>maven-war-plugin</artifactId>
117                <version>3.4.0</version>
118                <configuration>
119                    <packagingExcludes>pom.xml</packagingExcludes>
120                </configuration>
121            </plugin>
122            <!-- tag::maven-failsafe-plugin[] -->
123            <plugin>
124                <groupId>org.apache.maven.plugins</groupId>
125                <artifactId>maven-failsafe-plugin</artifactId>
126                <version>3.2.5</version>
127                <configuration>
128                  <systemPropertyVariables>
129                    <arquillian.war.name>${arquillian.war.name}.war</arquillian.war.name>
130                  </systemPropertyVariables>
131                </configuration>
132            </plugin>
133            <!-- end::maven-failsafe-plugin[] -->
134            <!-- tag::liberty-maven-plugin[] -->
135            <plugin>
136                <groupId>io.openliberty.tools</groupId>
137                <artifactId>liberty-maven-plugin</artifactId>
138                <version>3.10</version>
139                <configuration>
140                  <!-- tag::arquillianProperties[] -->
141                  <arquillianProperties>
142                    <javaVmArguments>
143                      -Dsystem.context.root=/${arquillian.war.name}
144                    </javaVmArguments>
145                    <!-- tag::allowConnectingToRunningServer[] -->
146                    <allowConnectingToRunningServer>true</allowConnectingToRunningServer>
147                    <!-- end::allowConnectingToRunningServer[] -->
148                </arquillianProperties>
149                    <!-- end::arquillianProperties[] -->
150                </configuration>
151            </plugin>
152            <!-- end::liberty-maven-plugin[] -->
153        </plugins>
154    </build>
155</project>

The WARNAME variable is used to name the web archive and is defined in the pom.xml file. This name is necessary if you don’t want a randomly generated web archive name.

The ShrinkWrap API is used to create the web archive. All of the packages in the inventory service must be added to the web archive; otherwise, the code compiles successfully but fails at runtime when the injection of the InventoryResource class takes place. You can learn about the ShrinkWrap archive configuration in this Arquillian guide.

The @ArquillianResource annotation is used to retrieve the http://localhost:9080/arquillian-managed/ base URL for this web service. The annotation provides the host name, port number and web archive information for this service, so you don’t need to hardcode these values in the test case. The arquillian-managed path in the URL comes from the WAR name you specified when you created the web archive in the @Deployment annotated method. It’s needed when the inventory service communicates with the system service to get the system properties.

The testInventoryEndpoints method is an integration test to test the inventory service endpoints. The @RunAsClient annotation added in this test case indicates that this test case is to be run on the client side. By running the tests on the client side, the tests are run against the managed container. The endpoint test case first calls the http://localhost:9080/{WARNAME}/inventory/systems/{hostname} endpoint with the localhost host name to add its system properties to the inventory. The test verifies that the system property for the local and service JVM match. Then, the test method calls the http://localhost:9080/{WARNAME}/inventory/systems endpoint. The test checks that the inventory has one host and that the host is localhost. The test also verifies that the system property stored in the inventory for the local and service JVM match.

Contexts and Dependency Injection (CDI) is used to inject an instance of the InventoryResource class into this test class. You can learn more about CDI in the Injecting dependencies into microservices guide.

The injected InventoryResource instance is then tested by the testInventoryResourceFunctions method. This test case calls the listContents() method to get all systems that are stored in this inventory and verifies that localhost is the only system being found. Notice the functional test case doesn’t store any system in the inventory, the localhost system is from the endpoint test case that ran before this test case. The @InSequence Arquillian annotation guarantees the test sequence. The sequence is important for the two tests, as the results in the first test impact the second one.

The test cases are ready to run. You will configure the Maven build and the Liberty configuration to run them.

Configuring Arquillian with Liberty

Configure your build to use the Arquillian Liberty Managed container and set up your Open Liberty to run your test cases by configuring the server.xml file.

Configuring your test build

First, configure your test build with Maven. All of the Maven configuration takes place in the pom.xml file, which is provided for you.

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 
  5    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6
  7    <modelVersion>4.0.0</modelVersion>
  8
  9    <groupId>io.openliberty.guides</groupId>
 10    <artifactId>guide-arquillian-managed</artifactId>
 11    <version>1.0-SNAPSHOT</version>
 12    <packaging>war</packaging>
 13
 14    <properties>
 15        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 16        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 17        <maven.compiler.source>11</maven.compiler.source>
 18        <maven.compiler.target>11</maven.compiler.target>
 19        <!-- Liberty configuration -->
 20        <liberty.var.http.port>9080</liberty.var.http.port>
 21        <liberty.var.https.port>9443</liberty.var.https.port>
 22        <!-- arquillian configuration -->
 23        <!-- tag::ArquillianWarName[] -->
 24        <arquillian.war.name>arquillian-managed</arquillian.war.name>
 25        <!-- end::ArquillianWarName[] -->
 26    </properties>
 27
 28    <dependencyManagement>
 29        <dependencies>
 30            <!-- tag::arquillian-bom[] -->
 31            <dependency>
 32                <groupId>org.jboss.arquillian</groupId>
 33                <artifactId>arquillian-bom</artifactId>
 34                <version>1.8.0.Final</version>
 35                <type>pom</type>
 36                <scope>import</scope>
 37            </dependency>
 38            <!-- end::arquillian-bom[] -->
 39        </dependencies>
 40    </dependencyManagement>
 41
 42    <dependencies>
 43        <!-- Provided dependencies -->
 44        <dependency>
 45            <groupId>jakarta.platform</groupId>
 46            <artifactId>jakarta.jakartaee-api</artifactId>
 47            <version>10.0.0</version>
 48            <scope>provided</scope>
 49        </dependency>
 50        <dependency>
 51            <groupId>org.eclipse.microprofile</groupId>
 52            <artifactId>microprofile</artifactId>
 53            <version>6.1</version>
 54            <type>pom</type>
 55            <scope>provided</scope>
 56        </dependency>
 57        <!-- For tests -->
 58        <dependency>
 59            <groupId>junit</groupId>
 60            <artifactId>junit</artifactId>
 61            <version>4.13.2</version>
 62            <scope>test</scope>
 63        </dependency>
 64        <dependency>
 65            <groupId>org.jboss.resteasy</groupId>
 66            <artifactId>resteasy-client</artifactId>
 67            <version>6.2.7.Final</version>
 68            <scope>test</scope>
 69        </dependency>
 70        <dependency>
 71            <groupId>org.jboss.resteasy</groupId>
 72            <artifactId>resteasy-json-binding-provider</artifactId>
 73            <version>6.2.7.Final</version>
 74            <scope>test</scope>
 75        </dependency>
 76        <dependency>
 77            <groupId>org.glassfish</groupId>
 78            <artifactId>jakarta.json</artifactId>
 79            <version>2.0.1</version>
 80            <scope>test</scope>
 81        </dependency>
 82        <!-- For JDK 9 & above Support -->
 83        <dependency>
 84            <groupId>javax.xml.bind</groupId>
 85            <artifactId>jaxb-api</artifactId>
 86            <version>2.3.1</version>
 87        </dependency>
 88        <dependency>
 89            <groupId>javax.activation</groupId>
 90            <artifactId>activation</artifactId>
 91            <version>1.1.1</version>
 92        </dependency>
 93        <!-- tag::arquillian-liberty-managed-junit[] -->
 94        <dependency>
 95            <groupId>io.openliberty.arquillian</groupId>
 96            <artifactId>arquillian-liberty-managed-jakarta-junit</artifactId>
 97            <version>2.1.3</version>
 98            <type>pom</type>
 99            <scope>test</scope>
100        </dependency>
101        <!-- end::arquillian-liberty-managed-junit[] -->
102        <!-- tag::shrinkwrap-api[] -->
103        <dependency>
104            <groupId>org.jboss.shrinkwrap</groupId>
105            <artifactId>shrinkwrap-api</artifactId>
106            <scope>test</scope>
107        </dependency>
108        <!-- end::shrinkwrap-api[] -->
109    </dependencies>
110
111    <build>
112        <finalName>${project.artifactId}</finalName>
113        <plugins>
114            <plugin>
115                <groupId>org.apache.maven.plugins</groupId>
116                <artifactId>maven-war-plugin</artifactId>
117                <version>3.4.0</version>
118                <configuration>
119                    <packagingExcludes>pom.xml</packagingExcludes>
120                </configuration>
121            </plugin>
122            <!-- tag::maven-failsafe-plugin[] -->
123            <plugin>
124                <groupId>org.apache.maven.plugins</groupId>
125                <artifactId>maven-failsafe-plugin</artifactId>
126                <version>3.2.5</version>
127                <configuration>
128                  <systemPropertyVariables>
129                    <arquillian.war.name>${arquillian.war.name}.war</arquillian.war.name>
130                  </systemPropertyVariables>
131                </configuration>
132            </plugin>
133            <!-- end::maven-failsafe-plugin[] -->
134            <!-- tag::liberty-maven-plugin[] -->
135            <plugin>
136                <groupId>io.openliberty.tools</groupId>
137                <artifactId>liberty-maven-plugin</artifactId>
138                <version>3.10</version>
139                <configuration>
140                  <!-- tag::arquillianProperties[] -->
141                  <arquillianProperties>
142                    <javaVmArguments>
143                      -Dsystem.context.root=/${arquillian.war.name}
144                    </javaVmArguments>
145                    <!-- tag::allowConnectingToRunningServer[] -->
146                    <allowConnectingToRunningServer>true</allowConnectingToRunningServer>
147                    <!-- end::allowConnectingToRunningServer[] -->
148                </arquillianProperties>
149                    <!-- end::arquillianProperties[] -->
150                </configuration>
151            </plugin>
152            <!-- end::liberty-maven-plugin[] -->
153        </plugins>
154    </build>
155</project>

Let’s look into each of the required elements for this configuration.

You need the arquillian-bom Bill of Materials. It’s a Maven artifact that defines the versions of Arquillian dependencies to make dependency management easier.

The arquillian-liberty-managed-junit dependency bundle, which includes all the core dependencies, is required to run the Arquillian tests on a managed Liberty container that uses JUnit. You can learn more about the Arquillian Liberty dependency bundles. The shrinkwrap-api dependency allows you to create your test archive, which is packaged into a WAR file and deployed to the Open Liberty.

The maven-failsafe-plugin artifact runs your Arquillian integration tests by using JUnit.

Lastly, specify the liberty-maven-plugin configuration that defines your Open Liberty runtime configuration. When the application runs in an Arquillian Liberty managed container, the name of the .war file is used as the context root of the application. You can pass context root information to the application and customize the container by using the arquillianProperties configuration. To allow connections to Liberty running in dev mode, set allowConnectingToRunningServer to true.

To learn more about the arquillianProperties configuration, see the Arquillian Liberty Managed documentation.

Configuring Liberty’s server.xml configuration file

Now that you’re done configuring your Maven build, set up your Open Liberty to run your test cases by configuring the server.xml configuration file.

Take a look at the server.xml file.

server.xml

 1<server description="new server">
 2
 3    <featureManager>
 4        <feature>restfulWS-3.1</feature>
 5        <feature>jsonb-3.0</feature>
 6        <feature>jsonp-2.1</feature>
 7        <feature>cdi-4.0</feature>
 8        <feature>mpConfig-3.1</feature>
 9
10        <!--Enable the following features to run tests with Arquillian managed container-->
11        <!-- tag::localConnector[] -->
12        <feature>localConnector-1.0</feature>
13        <!-- end::localConnector[] -->
14        <!-- tag::Servlet[] -->
15        <feature>servlet-6.0</feature>
16        <!-- end::Servlet[] -->
17        
18    </featureManager>
19
20    <variable name="http.port" defaultValue="9080" />
21    <variable name="https.port" defaultValue="9443" />
22
23    <httpEndpoint id="defaultHttpEndpoint" httpPort="${http.port}"
24        httpsPort="${https.port}" />
25    <webApplication location="guide-arquillian-managed.war"
26        context-root="/" />
27</server>

The localConnector feature is required by the Arquillian Liberty Managed container to connect to and communicate with the Open Liberty runtime. The servlet feature is required during the deployment of the Arquillian tests in which servlets are created to perform the in-container testing.

Open another command-line session and run the configure-arquillian goal from the start directory to integrate Arquillian and the Arquillian Liberty managed and remote containers with your existing project.

mvn liberty:configure-arquillian

Because you started Open Liberty in dev mode, all the changes were automatically picked up. You can run the tests by pressing the enter/return key from the command-line session where you started dev mode. Look for the following output:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running it.io.openliberty.guides.system.SystemArquillianIT
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.133 s - in it.io.openliberty.guides.system.SystemArquillianIT
[INFO] Running it.io.openliberty.guides.inventory.InventoryArquillianIT
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.297 s - in it.io.openliberty.guides.
...
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
...

Running the tests

It’s now time to build and run your Arquillian tests outside of dev mode. Exit dev mode by pressing CTRL+C in the command-line session where you ran Liberty in the previous section.

Run the Maven command to package the application. Then, run the Liberty Maven Plugin goals to create the Liberty instance, install the features, and deploy the application to the instance. The configure-arquillian goal configures your Arquillian container. You can learn more about this goal in the configure-arquillian goal documentation.

mvn clean package
mvn liberty:create liberty:install-feature
mvn liberty:configure-arquillian

Now, you can run your Arquillian tests with the Maven integration-test goal:

mvn failsafe:integration-test

In the test output, you can see that the Liberty instance launched, and that the web archive, arquillian-managed, started as an application in the instance. You can also see that the tests are running and that the results are reported.

After the tests stop running, the test application is automatically undeployed and the instance shuts down. You should then get a message indicating that the build and tests are successful.

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running it.io.openliberty.guides.system.SystemArquillianIT
...
[AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT   ] CWWKG0093A: Processing configuration drop-ins resource: guide-arquillian-managed/finish/target/liberty/wlp/usr/servers/defaultServer/configDropins/overrides/liberty-plugin-variable-config.xml
[INFO    ] CWWKE0002I: The kernel started after 0.854 seconds
[INFO    ] CWWKF0007I: Feature update started.
[AUDIT   ] CWWKZ0058I: Monitoring dropins for applications.
[INFO    ] Aries Blueprint packages not available. So namespaces will not be registered
[INFO    ] CWWKZ0018I: Starting application guide-arquillian-managed.
...
[INFO    ] SRVE0169I: Loading Web Module: guide-arquillian-managed.
[INFO    ] SRVE0250I: Web Module guide-arquillian-managed has been bound to default_host.
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/
[INFO    ] SESN0176I: A new session context will be created for application key default_host/
[INFO    ] SESN0172I: The session manager is using the Java default SecureRandom implementation for session ID generation.
[AUDIT   ] CWWKZ0001I: Application guide-arquillian-managed started in 1.126 seconds.
[INFO    ] CWWKO0219I: TCP Channel defaultHttpEndpoint has been started and is now listening for requests on host localhost  (IPv4: 127.0.0.1) port 9080.
[AUDIT   ] CWWKF0012I: The server installed the following features: [cdi-2.0, jaxrs-2.1, jaxrsClient-2.1, jndi-1.0, jsonp-1.1, localConnector-1.0, mpConfig-1.3, servlet-4.0].
[INFO    ] CWWKF0008I: Feature update completed in 2.321 seconds.
[AUDIT   ] CWWKF0011I: The defaultServer server is ready to run a smarter planet. The defaultServer server started in 3.175 seconds.
[INFO    ] CWWKZ0018I: Starting application arquillian-managed.
...
[INFO    ] SRVE0169I: Loading Web Module: arquillian-managed.
[INFO    ] SRVE0250I: Web Module arquillian-managed has been bound to default_host.
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/arquillian-managed/
...
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.133 s - in it.io.openliberty.guides.system.SystemArquillianIT
[INFO] Running it.io.openliberty.guides.inventory.InventoryArquillianIT
[INFO    ] CWWKZ0018I: Starting application arquillian-managed.
[INFO    ] CWWKZ0136I: The arquillian-managed application is using the archive file at the guide-arquillian-managed/finish/target/liberty/wlp/usr/servers/defaultServer/dropins/arquillian-managed.war location.
[INFO    ] SRVE0169I: Loading Web Module: arquillian-managed.
[INFO    ] SRVE0250I: Web Module arquillian-managed has been bound to default_host.
...
[INFO    ] Setting the server's publish address to be /inventory/
[INFO    ] SRVE0242I: [arquillian-managed] [/arquillian-managed] [io.openliberty.guides.inventory.InventoryApplication]: Initialization successful.
[INFO    ] Setting the server's publish address to be /system/
[INFO    ] SRVE0242I: [arquillian-managed] [/arquillian-managed] [io.openliberty.guides.system.SystemApplication]: Initialization successful.
[INFO    ] SRVE0242I: [arquillian-managed] [/arquillian-managed] [ArquillianServletRunner]: Initialization successful.
[AUDIT   ] CWWKT0017I: Web application removed (default_host): http://localhost:9080/arquillian-managed/
[INFO    ] SRVE0253I: [arquillian-managed] [/arquillian-managed] [ArquillianServletRunner]: Destroy successful.
[INFO    ] SRVE0253I: [arquillian-managed] [/arquillian-managed] [io.openliberty.guides.inventory.InventoryApplication]: Destroy successful.
[AUDIT   ] CWWKZ0009I: The application arquillian-managed has stopped successfully.
[INFO    ] SRVE9103I: A configuration file for a web server plugin was automatically generated for this server at guide-arquillian-managed/finish/target/liberty/wlp/usr/servers/defaultServer/logs/state/plugin-cfg.xml.
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.297 s - in it.io.openliberty.guides.inventory.InventoryArquillianIT
...
Stopping server defaultServer.
...
Server defaultServer stopped.
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.018 s
[INFO] Finished at: 2020-06-23T12:40:32-04:00
[INFO] ------------------------------------------------------------------------

Great work! You’re done!

You just built some functional and integration tests with the Arquillian managed container and ran the tests for your microservices on Open Liberty.

Try one of the related guides to learn more about the technologies that you come across in this guide.

Guide Attribution

Testing microservices with the Arquillian managed container by Open Liberty is licensed under CC BY-ND 4.0

Copy file contents
Copied to clipboard

Prerequisites:

Nice work! Where to next?

What did you think of this guide?

Extreme Dislike Dislike Like Extreme Like

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

Like Open Liberty? Star our repo on GitHub.

Star