Rechercher

Ce contenu n'est pas disponible dans la langue sélectionnée.

Chapter 4. Testing an application deployed on JBoss EAP

download PDF

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:

To add tests for an application deployed on a JBoss EAP server running on OpenShift Container Platform, follow these procedures:

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

Procedure

  1. Define the following properties in the pom.xml configuration file:

    <properties>
        ...
        <version.plugin.failsafe>3.2.2</version.plugin.failsafe>
    </properties>
  2. Add the dependency required for tests.

    <project>
        ...
        <dependencies>
            ...
            <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
  3. 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>${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.

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

Procedure

  1. Navigate to the <application_home> directory.
  2. Create a directory to store the test class.

    $ mkdir -p src/test/java/org/jboss/as/quickstarts/helloworld
  3. Navigate to the new directory.

    $ cd src/test/java/org/jboss/as/quickstarts/helloworld
  4. 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.

Next steps

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

Procedure

  1. Navigate to the <application_home> directory.
  2. Run the integration test by using the verify command with the integration-testing profile.

    $ mvn verify -Pintegration-testing

    Example output

    [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] ------------------------------------------------------------------------

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

Procedure

  1. Push the changes to your Git repository.
  2. Navigate to the <application_home> directory.
  3. Run the test by using the verify command, activating the integration-testing profile and specifying the URL to the application.

    $ mvn verify -Pintegration-testing -Dserver.host=https://$(oc get route helloworld --template='{{ .spec.host }}')
    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

    $ keytool -trustcacerts -keystore _<path-to-java-truststore>_ -storepass _<trust-store-password>_ -importcert -alias _<alias-for-the-certificate>_ -file _<path-to-certificate>_/_<certificate-name>_

    Example output

    [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] ------------------------------------------------------------------------





Revised on 2024-02-21 14:02:46 UTC

Red Hat logoGithubRedditYoutubeTwitter

Apprendre

Essayez, achetez et vendez

Communautés

À propos de la documentation Red Hat

Nous aidons les utilisateurs de Red Hat à innover et à atteindre leurs objectifs grâce à nos produits et services avec un contenu auquel ils peuvent faire confiance.

Rendre l’open source plus inclusif

Red Hat s'engage à remplacer le langage problématique dans notre code, notre documentation et nos propriétés Web. Pour plus de détails, consultez leBlog Red Hat.

À propos de Red Hat

Nous proposons des solutions renforcées qui facilitent le travail des entreprises sur plusieurs plates-formes et environnements, du centre de données central à la périphérie du réseau.

© 2024 Red Hat, Inc.