Thursday, 8 July 2021

Junit5: BeforeEach: Execute this method before every test

@BeforeEach is used to signal that the annotated method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, and @TestTemplate method in the current test class.

 

@BeforeEach methods must have a void return type, must not be private, and must not be static.

 

Is the method annotated with BeforeEach can have parameters?

Yes, it may optionally declare parameters to be resolved by ParameterResolvers.

 

Let’s see with an example.

 

BeforeEachDemo1.java

package com.sample.app;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BeforeEachDemo1 {
  
  @BeforeEach
  void beforeEach_1() {
    System.out.println("\nInside beforeEach_1");
  }
  
  @BeforeEach
  void beforeEach_2() {
    System.out.println("Inside beforeEach_2");
  }

  @Test
  public void test1() {
    System.out.println("Inside test1");
  }
  
  @Test
  public void test2() {
    System.out.println("Inside test2");
  }
}

 

When you run above application, you will see below messages in console.

Inside beforeEach_1
Inside beforeEach_2
Inside test1

Inside beforeEach_1
Inside beforeEach_2
Inside test2

 

Inheritance and Execution Order of BeforeEach method

@BeforeEach methods are inherited from superclasses as long as they are not overridden. @BeforeEach methods from superclasses will be executed before @BeforeEach methods in subclasses.

 

Let’s see it with an example.

 

BaseBeforeEachMethods.java

package com.sample.app;

import org.junit.jupiter.api.BeforeEach;

public class BaseBeforeEachMethods {

  @BeforeEach
  void setupSystem() {
    System.out.println("Setting up the system");
  }
  
  @BeforeEach
  void setupLogger() {
    System.out.println("Setting up logger");
  }
}

 

LoginTest.java

package com.sample.app;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class LoginTest extends BaseBeforeEachMethods{
  
  @BeforeEach
  void setupCreds() {
    System.out.println("Setting up user credentials");
  }

  @Test
  public void loginTest() {
    System.out.println("Inside login test\n");
  }
  
  @Test
  public void logoutTest() {
    System.out.println("Inside logout test\n");
  }
}

 

Run LoginTest application, you will see below messages in console.

Setting up logger
Setting up the system
Setting up user credentials
Inside login test

Setting up logger
Setting up the system
Setting up user credentials
Inside logout test

 

Is there any execution order guarantee for @BeforeEach methods?

JUnit does not guarantee the execution order of multiple @BeforeEach methods that are declared within a single test class or test interface. So, it is recommended to declare at most one @BeforeEach method and at most one @AfterEach method per test class or test interface unless there are no dependencies between the @BeforeEach methods or between the @AfterEach methods.

 

You can download all the applications from this link.

https://github.com/harikrishna553/junit5/tree/master/junit5-examples

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment