3.1 Açıklamalar @BeforeEach, @AfterEach
Önceki örnekte, her yöntemde bir nesne oluşturmak için kod yazmamız gerektiğini unutmayın.hesap makinesi.
Tabii ki, bu sadece bir satır, ancak gerçek sistemleri test edersek, genellikle birkaç düzine kod satırı alabilen birkaç nesne oluşturmamız ve yapılandırmamız gereken bir durumla karşılaşırız. Örnek:
//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());
Yukarıdaki örnekte, bir nesne oluşturduk ve yapılandırdık.HttpClientve send() yöntemini test etmek istiyorum .
Böylece test yönteminde her seferinde bir nesnenin oluşturulmasını yazmamakHttpClient, ayrı bir yönteme taşınabilir ve özel bir @BeforeEach notu verilebilir . Ardından Junit, her test yönteminden önce bu yöntemi çağırır. Örnek:
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());
}
}
Ayrıca bir sonraki test yönteminden sonra her seferinde çağrılacak özel bir yöntem oluşturabilir ve kullanılan kaynakları temizleyebilir , günlüğe bir şeyler yazabilirsiniz vb. Böyle bir yöntem @AfterEach notu ile işaretlenmelidir .
3 test yönteminiz test1() , test2() ve test3() varsa , arama sırası şöyle olacaktır:
- her yöntemden önce
- test1()
- AfterEach yöntemi
- her yöntemden önce
- deneme2()
- AfterEach yöntemi
- her yöntemden önce
- deneme3()
- AfterEach yöntemi
3.2 Açıklamalar @BeforeAll, @AfterAll
JUnit ayrıca , tüm test yöntemlerinden önce bir kez çağrılacak bir yöntem eklemenizi sağlar . Böyle bir yöntem, @BeforeAll ile açıklanmalıdır . Aynı zamanda eşleştirilmiş bir @AfterAll ek açıklamasına sahiptir . Bununla işaretlenen yöntem, tüm test yöntemlerinden sonra JUnit tarafından çağrılacaktır.
Her şeyin nasıl çalıştığını daha iyi anlamanızı sağlayan özel bir örnek yazalım. Hesap makinemizin testini esas alalım:
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");
}
}
Bu test, aşağıdaki metni konsola yazdıracaktır:
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