CodeGym/Java Course/Module 3. Java Professional/Setting up a test environment in JUnit

Setting up a test environment in JUnit

Available

3.1 Annotations @BeforeEach, @AfterEach

Note that in the previous example, in each method we had to write code to create an objectcalculator.

Of course, this is just one line, but if we test real systems, we will often have a situation where we need to create and configure several objects, which can take several dozen lines of code. Example:

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

In the example above, we have created and configured an objectHttpClientand want to test the send() method .

So that each time in the test method not to write the creation of an objectHttpClient, it can be moved to a separate method and given a special @BeforeEach annotation . Then Junit will call this method before each test method. Example:

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

You can also create a special method that will be called every time after the next test method, and clean up the used resources , write something to the log, etc. Such a method must be marked with the @AfterEach annotation .

If you have 3 test methods test1() , test2() and test3() , then the call order will be:

  • beforeEach method
  • test1()
  • AfterEach method
  • beforeEach method
  • test2()
  • AfterEach method
  • beforeEach method
  • test3()
  • AfterEach method

3.2 Annotations @BeforeAll, @AfterAll

JUnit also allows you to add a method that will be called once before all test methods . Such a method should be annotated with @BeforeAll . It also has a paired @AfterAll annotation . The method marked with it will be called by JUnit after all test methods.

Let's write a special example that allows you to better understand how it all works. Let's take testing of our calculator as a 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");
    }
}

This test will print the following text to the console:


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
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet