Maven does not run @BeforeEach Methods while running
By default Maven will not run the test with the Jupiter engine as
In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.
And this is not present by default.
So to enable it you have to configure the maven-surefire-plugin that runs the unit tests as documented in the Jupiter documentation :
UPDATE (28.10.2020):
Since version 2.22.0, you only have to specify a test dependency on the desired junit engine. Failing to do so, will also result in the behavior described in the question.
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Keeping the original answer as a reference, before version 2.22.0 the solution was:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Whatever the issue is not necessarily easy to spot because Maven uses a runner that is able to run the Jupiter tests but didn't manage to execute the hook methods...
As a hint : to know whether the JUnit 5 runner is launched you can execute the tests with the verbose flag such as : mvn test -X
.
If the Jupiter runner is used, you should find lines that look like :
[DEBUG] Surefire report directory: ...\target\surefire-reports
[DEBUG] Using configured provider org.junit.platform.surefire.provider.JUnitPlatformProvider
The first you need to do is to add a dependency like this:
<dependencies>
[...]
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
[...]
</dependencies>
The next thing is to use the most recent version of maven-surefire-plugin/maven-failsafe-plugin like this:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<excludes>
<exclude>some test to exclude here</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Starting with version 2.22.2 surefire supports JUnit 5...
Update:
- Use most recent version of maven-surefire/failsafe-plugin
maven-surefire-plugin by default takes all test classes which have the pattern *Test.java, in your case you should rename the class SomethingTest and it should be OK
https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html