3.1 Chú thích @B BeforeEach, @afterEach
Lưu ý rằng trong ví dụ trước, trong mỗi phương thức, chúng ta phải viết mã để tạo một đối tượngmáy tính.
Tất nhiên, đây chỉ là một dòng, nhưng nếu chúng ta kiểm tra các hệ thống thực, chúng ta sẽ thường gặp tình huống cần tạo và định cấu hình một số đối tượng, có thể mất vài chục dòng mã. Ví dụ:
//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
(
)
)
;
Trong ví dụ trên, chúng ta đã tạo và cấu hình một đối tượngHttpClientvà muốn kiểm tra phương thức send() .
Vì vậy, mỗi lần trong phương pháp thử nghiệm không viết việc tạo một đối tượngHttpClient, nó có thể được chuyển sang một phương thức riêng biệt và được cung cấp chú thích @B BeforeEach đặc biệt . Sau đó, Junit sẽ gọi phương thức này trước mỗi phương thức kiểm tra. Ví dụ:
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
(
)
)
;
}
}
Bạn cũng có thể tạo một phương thức đặc biệt sẽ được gọi mỗi lần sau phương thức thử nghiệm tiếp theo và dọn sạch các tài nguyên đã sử dụng , ghi nội dung nào đó vào nhật ký, v.v. Phương thức như vậy phải được đánh dấu bằng chú thích @AfterEach .
Nếu bạn có 3 phương thức test test1() , test2() và test3() thì lệnh gọi sẽ là:
- phương thức beforeEach
- kiểm tra1()
- Phương pháp AfterEach
- phương thức beforeEach
- kiểm tra2()
- Phương pháp AfterEach
- phương thức beforeEach
- kiểm tra3()
- Phương pháp AfterEach
3.2 Chú thích @B BeforeAll, @afterAll
JUnit cũng cho phép bạn thêm một phương thức sẽ được gọi một lần trước tất cả các phương thức kiểm tra . Một phương pháp như vậy phải được chú thích bằng @ BeforeAll . Nó cũng có một chú thích @afterAll được ghép nối . Phương thức được đánh dấu bằng nó sẽ được gọi bởi JUnit sau tất cả các phương thức kiểm tra.
Hãy viết một ví dụ đặc biệt cho phép bạn hiểu rõ hơn về cách thức hoạt động của tất cả. Hãy thử nghiệm máy tính của chúng tôi làm cơ sở:
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");
}
}
Bài kiểm tra này sẽ in văn bản sau ra bàn điều khiển:
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
GO TO FULL VERSION