4.2. アプリケーションをテストするためのテストクラスの作成
OpenShift Container Platform 上の JBoss EAP でアプリケーションがデプロイおよび実行されていることを検証する統合テストを作成します。このテストでは、アプリケーションの Web ページの HTTP GET が 200 OK を返すことを確認します。
この手順では、<application_home> は、アプリケーションの pom.xml
設定ファイルが含まれるディレクトリーを参照します。
前提条件
アプリケーションを JBoss EAP にデプロイしている。
詳細は、アプリケーションのビルドとサーバーへのデプロイ を参照してください。
JUnit テストに必要な Maven 依存関係を追加している。
詳細は、統合テストに必要な Maven 依存関係とプロファイルの追加 を参照してください。
手順
- <application_home> ディレクトリーに移動します。
テストクラスを格納するディレクトリーを作成します。
$ mkdir -p src/test/java/org/jboss/as/quickstarts/helloworld
新しいディレクトリーに移動します。
$ cd src/test/java/org/jboss/as/quickstarts/helloworld
デプロイメントをテストする Java クラス
HelloWorldServletIT.java
を作成します。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 } }
次のステップ
ベアメタル上で実行されている JBoss EAP サーバーにデプロイしたアプリケーションをテストするには、次の手順に従います。
OpenShift Container Platform 上で実行されている JBoss EAP サーバーにデプロイしたアプリケーションをテストするには、次の手順に従います。