Chapter 4. Testing an application deployed on JBoss EAP To ensure that the Hello World application deployed on JBoss EAP is working, you can add integration tests. To add tests for an application deployed on a JBoss EAP server running on bare metal, follow these procedures: Adding the Maven dependencies and profile required for integration tests Creating a test class to test an application Testing an application deployed on JBoss EAP that is running on bare metal To add tests for an application deployed on a JBoss EAP server running on OpenShift Container Platform, follow these procedures: Adding the Maven dependencies and profile required for integration tests Creating a test class to test an application Testing an application deployed to JBoss EAP on OpenShift Container Platform 4.1. Adding the Maven dependencies and profile required for integration tests To create integration tests for your applications, add the required Maven dependencies. Prerequisites You have created a Maven project. For more information, see Creating a Maven project for a hello world application . Procedure Define the following properties in the pom.xml configuration file: <properties> ... <version.plugin.failsafe>3.2.2</version.plugin.failsafe> </properties> Add the dependency required for tests. <project> ... <dependencies> ... <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project> Define a profile to add the plug-ins required for integration tests. <project> ... <profiles> ... <profile> <id>integration-testing</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> 1 <version>USD{version.plugin.failsafe}</version> <configuration> <includes> <include>**/HelloWorldServletIT</include> 2 </includes> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project> 1 Maven plug-in for running integration tests. 2 The name of the Java class that tests the application. steps Creating a test class to test an application 4.2. Creating a test class to test an application Create an integration test that verifies that the application is deployed and running on JBoss EAP on OpenShift Container Platform, by checking that the HTTP GET of its web page returns 200 OK. In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application. Prerequisites You have deployed your application to JBoss EAP. For more information, see Building and deploying an application to the server . You have added the Maven dependencies required for JUnit tests. For more information, see Adding the Maven dependencies and profile required for integration tests . Procedure Navigate to the <application_home> directory. Create a directory to store the test class. Navigate to the new directory. Create a Java class HelloWorldServletIT.java that tests the deployment. package org.jboss.as.quickstarts.helloworld; import org.junit.Test; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import static org.junit.Assert.assertEquals; public class HelloWorldServletIT { private static final String DEFAULT_SERVER_HOST = "http://localhost:8080/helloworld"; 1 @Test public void testHTTPEndpointIsAvailable() throws IOException, InterruptedException, URISyntaxException { String serverHost = System.getProperty("server.host"); if (serverHost == null) { serverHost = DEFAULT_SERVER_HOST; } final HttpRequest request = HttpRequest.newBuilder() .uri(new URI(serverHost+"/HelloWorld")) .GET() .build(); 2 final HttpClient client = HttpClient.newBuilder() .followRedirects(HttpClient.Redirect.ALWAYS) .connectTimeout(Duration.ofMinutes(1)) .build(); 3 final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); 4 assertEquals(200, response.statusCode()); 5 } } 1 The URL at which the application is running. This value is used if sever.host is undefined. 2 Create an HttpRequest instance for the application URI. 3 Create an HttpClient to send requests to and receive response from the application. 4 Get response from the application. 5 Test that the response revieved from the application is "200" indicating that the application is rechable. steps To test an application deployed on a JBoss EAP server running on bare metal, follow this procedure: Testing an application deployed on JBoss EAP that is running on bare metal To test an application deployed on a JBoss EAP server running on OpenShift Container Platform, follow this procedure: Testing an application deployed to JBoss EAP on OpenShift Container Platform 4.3. Testing an application deployed on JBoss EAP that is running on bare metal Test the application deployed on JBoss EAP that is running on bare metal. Prerequisites You have created a test class. For more information, see Creating a test class to test an application The application to test is deployed on JBoss EAP. JBoss EAP is running. Procedure Navigate to the <application_home> directory. Run the integration test by using the verify command with the integration-testing profile. Example output 4.4. Testing an application deployed to JBoss EAP on OpenShift Container Platform Test the application deployed to JBoss EAP on OpenShift Container Platform. Prerequisites You have created a test class. For more information, see Creating a test class to test an application Procedure Push the changes to your Git repository. Navigate to the <application_home> directory. Run the test by using the verify command, activating the integration-testing profile and specifying the URL to the application. Note The tests use SSL/TLS to connect to the deployed application. Therefore, you need the certificates to be trusted by the machine the tests are run from. To trust the certificates, you must add it to a Java trust store. Example Example output Revised on 2024-02-21 14:02:46 UTC

Commands:
<properties> <version.plugin.failsafe>3.2.2</version.plugin.failsafe> </properties>
<project> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
<project> <profiles> <profile> <id>integration-testing</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> 1 <version>USD{version.plugin.failsafe}</version> <configuration> <includes> <include>**/HelloWorldServletIT</include> 2 </includes> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
mkdir -p src/test/java/org/jboss/as/quickstarts/helloworld
cd src/test/java/org/jboss/as/quickstarts/helloworld
package org.jboss.as.quickstarts.helloworld; import org.junit.Test; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import static org.junit.Assert.assertEquals; public class HelloWorldServletIT { private static final String DEFAULT_SERVER_HOST = "http://localhost:8080/helloworld"; 1 @Test public void testHTTPEndpointIsAvailable() throws IOException, InterruptedException, URISyntaxException { String serverHost = System.getProperty("server.host"); if (serverHost == null) { serverHost = DEFAULT_SERVER_HOST; } final HttpRequest request = HttpRequest.newBuilder() .uri(new URI(serverHost+"/HelloWorld")) .GET() .build(); 2 final HttpClient client = HttpClient.newBuilder() .followRedirects(HttpClient.Redirect.ALWAYS) .connectTimeout(Duration.ofMinutes(1)) .build(); 3 final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); 4 assertEquals(200, response.statusCode()); 5 } }
mvn verify -Pintegration-testing
[INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.982 s [INFO] Finished at: 2023-11-22T14:53:54+05:30 [INFO] ------------------------------------------------------------------------
mvn verify -Pintegration-testing -Dserver.host=https://USD(oc get route helloworld --template='{{ .spec.host }}')
keytool -trustcacerts -keystore _<path-to-java-truststore>_ -storepass _<trust-store-password>_ -importcert -alias _<alias-for-the-certificate>_ -file _<path-to-certificate>_/_<certificate-name>_
[INFO] Running org.jboss.as.quickstarts.helloworld.HelloWorldServletIT [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.345 s -- in org.jboss.as.quickstarts.helloworld.HelloWorldServletIT [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-failsafe-plugin:3.2.2:verify (default) @ helloworld --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.984 s [INFO] Finished at: 2023-11-30T15:51:22+05:30 [INFO] ------------------------------------------------------------------------