CodeGym/Java Course/Module 3 a ɛto so abien/在 JUnit 中設置測試環境

在 JUnit 中設置測試環境

開放

3.1 註解@BeforeEach、@AfterEach

請注意,在前面的示例中,我們必須在每個方法中編寫代碼來創建一個對象計算器.

當然這只是一行,但是如果我們測試真實的系統,經常會出現需要創建和配置幾個對象的情況,這可能需要幾十行代碼。例子:

//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(),那麼調用順序將是:

  • beforeEach 方法
  • 測試1()
  • AfterEach 方法
  • beforeEach 方法
  • 測試2()
  • AfterEach 方法
  • beforeEach 方法
  • 測試 3()
  • AfterEach 方法

3.2 註解@BeforeAll、@AfterAll

JUnit 還允許您添加一個將在所有測試方法之前調用一次的方法。這樣的方法應該用@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
留言
  • 受歡迎
你必須登入才能留言
此頁面尚無留言