JUnit 5 Tutorial – @BeforeEach Annotation

This is a continuation of the JUnit 5 tutorial. The introduction is posted here.

annotation @BeforeEach used to indicate that an annotated method should be executed before each method @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory in the current class.

JUnit 5 annotation @BeforeEach is one of the lifecycle methods and replaces annotation @Before in JUnit 4.

By default, test methods will execute on the same thread as annotated @BeforeEach method.

1. Using @BeforeEach

@BeforeEach
public void initEach(){
     //test setup code
}

@Test
void succeedingTest() {
    //test code and assertions
}
@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

Method @BeforeEach inherits from parent classes (or interfaces) as long as they are not hidden Or no redefined

In addition, each method annotated @Before from parent classes (or interfaces) will be executed front each method with annotation @Before in child classes.

2. Example @BeforeEach

We used Calculator class and added one method add()

Testing 5 times with annotation @RepeatedTest… annotation @RepeatedTest will cause the test to run add() Five times.

Annotated @BeforeEach the method must be executed each time the test method is called.

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

This is the calculator class:

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

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

Выход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

Obviously the annotated @BeforeEach method initEach() called once when calling the test method

Have a nice study !!!

Download source code

Similar Posts

Leave a Reply