JUnit 5 does not execute method annotated with BeforeEach

In my case the problem was that the @Test annotation was taken from wrong import. Originally it was imported from org.junit.Test. Once I have switched it to org.junit.jupiter.api.Test the problem was resolved.

Wrong original code:

import org.junit.Test;

@BeforeEach
...some code

@Test
...some code

Correct fixed code:

import org.junit.jupiter.api.Test;

@BeforeEach
...some code

@Test
...some code

Your init() method is not invoked because you have not instructed Maven Surefire to use the JUnit Platform Surefire Provider.

Thus, surprisingly your test is not even being run with JUnit. Instead, it is being run with Maven Surefire's support for what they call POJO Tests.

Adding the following to your pom.xml should solve the problem.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.1.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Tags:

Java

Maven

Junit5