CodeGym/Java Course/モジュール 3/JUnit でのテスト環境のセットアップ

JUnit でのテスト環境のセットアップ

使用可能

3.1 アノテーション @BeforeEach、@AfterEach

前の例では、各メソッドでオブジェクトを作成するコードを記述する必要があることに注意してください。電卓

もちろん、これは 1 行にすぎませんが、実際のシステムをテストする場合は、複数のオブジェクトを作成して構成する必要があることがよくあり、それには数十行のコードが必要になることがあります。例:

//Create an HttpClient object
   HttpClient client = HttpClient.newBuilder()
    	.version(Version.HTTP_1_1)
    	.followRedirects(Redirect.NORMAL)
        .connectTimeout(Duration.ofSeconds(20))
    	.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
        .authenticator(Authenticator.getDefault())
    	.build();

   //Create an HttpRequest object
  HttpRequest request = HttpRequest.newBuilder()
     .uri(new URI("https://codegym.cc"))
     .headers("Content-Type", " application/octet-stream")
     .POST( HttpRequest.BodyPublishers. ofInputStream ( () -> is; ))
     .build();

   //Call the send() method
   HttpResponse response = client.send(request, BodyHandlers.ofString());
   System.out.println(response.statusCode());
   System.out.println(response.body());

上の例では、オブジェクトを作成して構成しました。HTTPクライアントsend()メソッドをテストしたいと考えています。

テストメソッド内で毎回オブジェクトの作成を書かないようにするHTTPクライアント場合は、別のメソッドに移動し、特別な@BeforeEachアノテーションを付けることができます。その後、Junit は各テスト メソッドの前にこのメソッドを呼び出します。例:

class HttpClientTest {
     	public HttpClient client;

   	@BeforeEach
    	public void init(){
   	   client = HttpClient.newBuilder()
   	        .version(Version.HTTP_1_1)
   	        .followRedirects(Redirect.NORMAL)
   	        .connectTimeout(Duration.ofSeconds(20))
   	        .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
   	        .authenticator(Authenticator.getDefault())
   	        .build();
    	}

   	@Test
    	public void send200() throws Exception {
     	   //Create an HttpRequest() object
         	HttpRequest request = HttpRequest.newBuilder(new URI("https://codegym.cc")).build();

     	   //Call the send() method
     	   HttpResponse response = client.send(request, BodyHandlers.ofString());
        	assertEquals(200, response.statusCode());
        }

   	@Test
    	public void send404() throws Exception {
     	   //Create an HttpRequest() object
         	HttpRequest request = HttpRequest.newBuilder(new URI("https://codegym.cc/unknown")).build();

     	   //Call the send() method
     	   HttpResponse response = client.send(request, BodyHandlers.ofString());
        	assertEquals(404, response.statusCode());
    	}
}

次のテスト メソッドの後に毎回呼び出される特別なメソッドを作成し、使用されたリソースをクリーンアップしたり、ログに何かを書き込んだりすることもできます。そのようなメソッドには@AfterEachアノテーションを付ける必要があります。

3 つのテスト メソッドtest1()test2()test3()がある場合、呼び出し順序は次のようになります。

  • before各メソッド
  • テスト1()
  • 各メソッドの後
  • before各メソッド
  • テスト2()
  • 各メソッドの後
  • before各メソッド
  • テスト3()
  • 各メソッドの後

3.2 アノテーション @BeforeAll、@AfterAll

JUnit では、すべてのテスト メソッドの前に 1 回呼び出されるメソッドを追加することもできます。このようなメソッドには@BeforeAllの注釈を付ける必要があります。ペアになった@AfterAllアノテーションもあります。これでマークされたメソッドは、すべてのテスト メソッドの後で JUnit によって呼び出されます。

すべてがどのように機能するかをよりよく理解できるようにする特別な例を書いてみましょう。基礎として電卓をテストしてみましょう。

class CalculatorTest {
    private Calculator calc = new Calculator();

    @BeforeAll
    public static void init(){
        System.out.println("BeforeAll init() method called");
    }

    @BeforeEach
    public void initEach(){
        System.out.println("BeforeEach initEach() method called");
    }

    @Test
    public void add(){
        System.out.println("Testing Addition");
    }

    @Test
    public void sub() {
        System.out.println("Testing Subtraction");
    }

    @Test
    public void mul(){
        System.out.println("Testing Multiplication");
    }

    @Test
    public void div() {
        System.out.println("Testing Division");
    }
}

このテストでは、次のテキストがコンソールに出力されます。


BeforeAll init() method called
BeforeEach initEach() method called
Testing Addition
 
BeforeEach initEach() method called
Testing Subtraction
 
BeforeEach initEach() method called
Testing Multiplication
 
BeforeEach initEach() method called
Testing Division
コメント
  • 人気
  • 新規
  • 古い
コメントを残すには、サインインしている必要があります
このページにはまだコメントがありません