You can test this service manually by starting a server and pointing a web browser at the http://localhost:9080/LibertyProject/System/properties URL. However, automated tests are a much better approach because they trigger a failure if a change introduces a bug. JUnit and the JAX-RS Client API provide a simple environment to test the application.

You can write tests for the individual units of code outside of a running application server, or they can be written to call the application server directly. In this example, you will create a test that does the latter.

Create the EndpointIT class. src/test/java/it/io/openliberty/guides/rest/EndpointIT.java

EndpointIT.java

This test class has more lines of code than the resource implementation. This situation is common. The test method is indicated with the @Test annotation.

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> guide-rest-intro </artifactId> 9 <version> 1.0-SNAPSHOT </version> 10 <packaging> war </packaging> 11 12 <properties> 13 <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> 14 <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding> 15 <maven.compiler.source> 1.8 </maven.compiler.source> 16 <maven.compiler.target> 1.8 </maven.compiler.target> 17 18 19 <liberty.var.default.http.port> 9080 </liberty.var.default.http.port> 20 21 22 <liberty.var.default.https.port> 9443 </liberty.var.default.https.port> 23 24 25 <liberty.var.app.context.root> LibertyProject </liberty.var.app.context.root> 26 27 </properties> 28 29 <dependencies> 30 31 <dependency> 32 <groupId> jakarta.platform </groupId> 33 <artifactId> jakarta.jakartaee-api </artifactId> 34 <version> 8.0.0 </version> 35 <scope> provided </scope> 36 </dependency> 37 <dependency> 38 <groupId> org.eclipse.microprofile </groupId> 39 <artifactId> microprofile </artifactId> 40 <version> 3.3 </version> 41 <type> pom </type> 42 <scope> provided </scope> 43 </dependency> 44 45 <dependency> 46 <groupId> org.junit.jupiter </groupId> 47 <artifactId> junit-jupiter </artifactId> 48 <version> 5.6.2 </version> 49 <scope> test </scope> 50 </dependency> 51 <dependency> 52 <groupId> org.apache.cxf </groupId> 53 <artifactId> cxf-rt-rs-client </artifactId> 54 <version> 3.3.6 </version> 55 <scope> test </scope> 56 </dependency> 57 <dependency> 58 <groupId> org.apache.cxf </groupId> 59 <artifactId> cxf-rt-rs-extension-providers </artifactId> 60 <version> 3.3.6 </version> 61 <scope> test </scope> 62 </dependency> 63 <dependency> 64 <groupId> org.eclipse </groupId> 65 <artifactId> yasson </artifactId> 66 <version> 1.0.7 </version> 67 <scope> test </scope> 68 </dependency> 69 </dependencies> 70 71 <build> 72 <finalName> ${project.artifactId} </finalName> 73 <plugins> 74 75 <plugin> 76 <groupId> io.openliberty.tools </groupId> 77 <artifactId> liberty-maven-plugin </artifactId> 78 <version> 3.2 </version> 79 </plugin> 80 81 <plugin> 82 <groupId> org.apache.maven.plugins </groupId> 83 <artifactId> maven-failsafe-plugin </artifactId> 84 <version> 2.22.2 </version> 85 <configuration> 86 87 <systemPropertyVariables> 88 <http.port> ${liberty.var.default.http.port} </http.port> 89 <context.root> ${liberty.var.app.context.root} </context.root> 90 </systemPropertyVariables> 91 92 </configuration> 93 </plugin> 94 <plugin> 95 <groupId> org.apache.maven.plugins </groupId> 96 <artifactId> maven-war-plugin </artifactId> 97 <version> 3.2.3 </version> 98 </plugin> 99 100 <plugin> 101 <groupId> org.apache.maven.plugins </groupId> 102 <artifactId> maven-surefire-plugin </artifactId> 103 <version> 2.22.2 </version> 104 </plugin> 105 </plugins> 106 </build> 107 </project>

The test code needs to know some information about the application to make requests. The server port and the application context root are key, and are dictated by the server configuration. While this information can be hardcoded, it is better to specify it in a single place like the Maven pom.xml file. Refer to the pom.xml file to see how the application information such as the default.http.port , default.https.port and app.context.root elements are provided in the file.

These Maven properties are then passed to the Java test program as the <systemPropertyVariables/> element in the pom.xml file.

Getting the values to create a representation of the URL is simple. The test class uses the getProperty method to get the application details.

To call the JAX-RS service using the JAX-RS client, first create a WebTarget object by calling the target method that provides the URL. To cause the HTTP request to occur, the request().get() method is called on the WebTarget object. The get method call is a synchronous call that blocks until a response is received. This call returns a Response object, which can be inspected to determine whether the request was successful.

The first thing to check is that a 200 response was received. The JUnit assertEquals method can be used for this check.

Check the response body to ensure it returned the right information. Since the client and the server are running on the same machine, it is reasonable to expect that the system properties for the local and remote JVM would be the same. In this case, an assertEquals assertion is made so that the os.name system property for both JVMs is the same. You can write additional assertions to check for more values.