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());
ऊपर दिए गए उदाहरण में, हमने एक ऑब्जेक्ट बनाया और कॉन्फ़िगर किया हैएचटीपी क्लाइंटऔर भेजें() विधि का परीक्षण करना चाहते हैं ।
ताकि हर बार परीक्षण पद्धति में किसी वस्तु के निर्माण को न लिखेंएचटीपी क्लाइंट, इसे एक अलग विधि में ले जाया जा सकता है और एक विशेष @BeforeEach एनोटेशन दिया जा सकता है । तब जूनिट प्रत्येक परीक्षण विधि से पहले इस विधि को कॉल करेगा। उदाहरण:
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() , तो कॉल ऑर्डर होगा:
- प्रत्येक विधि से पहले
- परीक्षण 1 ()
- प्रत्येक विधि के बाद
- प्रत्येक विधि से पहले
- परीक्षण 2 ()
- प्रत्येक विधि के बाद
- प्रत्येक विधि से पहले
- टेस्ट3 ()
- प्रत्येक विधि के बाद
3.2 एनोटेशन @BeforeAll, @AfterAll
JUnit आपको एक विधि जोड़ने की भी अनुमति देता है जिसे सभी परीक्षण विधियों से पहले एक बार कॉल किया जाएगा । ऐसी विधि को @BeforeAll के साथ एनोटेट किया जाना चाहिए । इसमें एक जोड़ा @AfterAll एनोटेशन भी है । इसके साथ चिह्नित विधि सभी परीक्षण विधियों के बाद जुनीट द्वारा बुलाई जाएगी।
आइए एक विशेष उदाहरण लिखें जो आपको बेहतर ढंग से समझने की अनुमति देता है कि यह सब कैसे काम करता है। आइए आधार के रूप में हमारे कैलकुलेटर का परीक्षण करें:
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
GO TO FULL VERSION