JUnit 5 @BeforeEach

In JUnit 5, @BeforeEach annotation is used to signal that the annotated method should be executed before each invocation of @Test@RepeatedTest@ParameterizedTest, or @TestFactory method in the current class.

The @BeforeEach annotation is one of the test lifecycle methods and is the replacement of @Before annotation in JUnit 4.

By default, the test methods will be executed in the same thread as @BeforeEach annotated method.

1. @BeforeEach Usage

  • Annotate a method with @BeforeEach as given below:
@BeforeEach
public void initEach(){
     //test setup code
}

@Test
void succeedingTest() {
    //test code and assertions
}
  • @BeforeEach annotated method MUST NOT be a static method otherwise it will throw runtime error.
@BeforeEach
public static void initEach(){
     //test setup code
}

//Error


org.junit.platform.commons.JUnitException: @BeforeEach method 'public static void com.howtodoinjava.junit5.examples. JUnit5AnnotationsExample.initEach()' must not be static.
at org.junit.jupiter.engine.descriptor. LifecycleMethodUtils.assertNonStatic(LifecycleMethodUtils.java:73)

@BeforeEach in parent and child classes

@BeforeEach method is inherited from parent classes (or interfaces) as long as they are not hidden or overridden

Furthermore, @BeforeEach annotated method from parent class (or interface) will be executed before the method in child class.

2. @BeforeEach Example

We have used the Calculator class and added one add() method.

We will test it 5 times using @RepeatedTest annotation. @RepeatedTest annotation will cause the add() test to run 5 times.

@BeforeEach annotated method should execute for each invocation of the test method.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

public class BeforeEachTest {

    @DisplayName("Add operation test")
    @RepeatedTest(5)
    void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) {

        System.out.println("Running test -> " + repetitionInfo.getCurrentRepetition());
        Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2");
    }

    @BeforeAll
    public static void init(){
        System.out.println("BeforeAll init() method called");
    }

    @BeforeEach
    public void initEach(){
        System.out.println("BeforeEach initEach() method called");
    }
}

Where Calculator class is:

public class Calculator
{
    public int add(int a, int b) {
        return a + b;
    }
}

Now execute the test and you will see below console output:

BeforeAll init() method called
BeforeEach initEach() method called

BeforeEach initEach() method called
Running test -> 1

BeforeEach initEach() method called
Running test -> 2

BeforeEach initEach() method called
Running test -> 3

BeforeEach initEach() method called
Running test -> 4

BeforeEach initEach() method called
Running test -> 5

Clearly, @BeforeEach annotated initEach() method is called once per test method invocation.

Happy Learning !!

Sourcecode Download

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode