CodeGym /Java 课程 /模块 3 /在 JUnit 中设置测试环境

在 JUnit 中设置测试环境

模块 3
第 3 级 , 课程 2
可用

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
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION