CodeGym /Kursus Jawa /3. modul /Nggawe lingkungan test ing JUnit

Nggawe lingkungan test ing JUnit

3. modul
tingkat , wulangan
kasedhiya

3.1 Anotasi @BeforeEach, @AfterEach

Elinga yen ing conto sadurunge, ing saben cara kita kudu nulis kode kanggo nggawe obyekkalkulator.

Mesthi, iki mung siji baris, nanging yen kita nyoba sistem nyata, kita bakal kerep duwe kahanan ngendi kita kudu nggawe lan ngatur sawetara obyek, kang bisa njupuk sawetara rolas baris kode. Tuladha:

//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());

Ing conto ing ndhuwur, kita wis nggawe lan ngatur obyekHttpClientlan pengin nyoba cara ngirim () .

Supaya saben wektu ing cara test ora nulis nggawe obyekHttpClient, bisa dipindhah menyang cara sing kapisah lan diwenehi anotasi @BeforeEach khusus . Banjur Junit bakal nelpon cara iki sadurunge saben cara tes. Tuladha:

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());
    	}
}

Sampeyan uga bisa nggawe cara khusus sing bakal disebut saben wektu sawise cara test sabanjuré, lan ngresiki munggah sumber daya digunakake , nulis soko kanggo log, etc.. Cara kuwi kudu ditandhani karo @AfterEach anotasi .

Yen sampeyan duwe 3 metode test test1() , test2() lan test3() , banjur urutan telpon bakal:

  • sadurunge Saben metode
  • tes 1()
  • Sawise saben metode
  • sadurunge Saben metode
  • tes 2()
  • Sawise saben metode
  • sadurunge Saben metode
  • tes 3()
  • Sawise saben metode

3.2 Anotasi @BeforeAll, @AfterAll

JUnit uga ngidini sampeyan nambahake metode sing bakal diarani sepisan sadurunge kabeh metode tes . Cara kasebut kudu dianotasi karo @BeforeAll . Uga nduweni anotasi @AfterAll sing dipasangake . Cara sing ditandhani bakal diarani JUnit sawise kabeh metode tes.

Ayo nulis conto khusus sing ngidini sampeyan luwih ngerti cara kerjane. Ayo dadi tes kalkulator minangka basis:

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");
    }
}

Tes iki bakal nyithak teks ing ngisor iki menyang konsol:


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