Surefire Is Not Picking Up Junit 5 Tests

Maven not running JUnit 5 tests

According to the annotation (import org.junit.jupiter.api.Test), you are trying to run JUnit 5 tests with Maven. According to the documentation, you have to add this dependency:

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>

Your version of Maven comes with a version of maven-surefire-plugin which does not support JUnit 5. You could update your Maven to the latest version. You could also set the version of the maven-surefire-plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.0</version>
</plugin>

See the junit5-samples for this information.

See the Maven Surefire Plugin artifact in a Maven repository. At version 3.0.0-M3 as of 2019-01.

Surefire is not picking up Junit 5 tests

The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206.

As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-provider provider and the TestEngine implementation for the new API:

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<!-- latest version (2.20.1) does not work well with JUnit5 -->
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Also, be sure to declare the junit-jupiter-api dependency with a scope of test:

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>

Maven Surefire not running JUnit 5 tests

There are two things. First you should upgrade jacoco dependency to 0.8.5 otherwise it fails based on the JDK14 requirement. (Created an pull request to your repository). If those things are configured appropriately the result is this:

I would also recommend to upgrade maven-compiler-plugin to most recent version and also all other plugins.

As you can see you have WARNING's in your build which should be fixed and failing tests.

This build has been run on plain command line also with JUnit-Jupiter 5.7.0 which works perfectly.

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/classes
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java uses or overrides a deprecated API.
[WARNING] /Users/khmarbaise/ws-git-so/prog3-sose2020/src/main/java/bankprojekt/verarbeitung/Kunde.java: Recompile with -Xlint:deprecation for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ prog3-sose2020 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/khmarbaise/ws-git-so/prog3-sose2020/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ prog3-sose2020 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ prog3-sose2020 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running bankprojekt.GirokontoTest
[ERROR] Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.087 s <<< FAILURE! - in bankprojekt.GirokontoTest
[ERROR] bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel Time elapsed: 0.052 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <448.87081188037814> but was: <400.0>
at bankprojekt.GirokontoTest.abhebenMitWaehrungswechsel(GirokontoTest.java:31)

[INFO] Running bankprojekt.WaehrungTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in bankprojekt.WaehrungTest
Kunde Mustermann, Max zerst�rt
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] GirokontoTest.abhebenMitWaehrungswechsel:31 expected: <448.87081188037814> but was: <400.0>
[INFO]
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.792 s
[INFO] Finished at: 2020-09-14T17:50:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M4:test (default-test) on project prog3-sose2020: There are test failures.
[ERROR]
[ERROR] Please refer to /Users/khmarbaise/ws-git-so/prog3-sose2020/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

Maven Surefire not detecting Junit5

The most important thing is to follow conventions in Apache Maven. So do not change the configuration for source directory and the test directory that makes in 99.999% of the cases no sense.

So first remove the following configuration parts:

<finalName>Estrazioni-Batch</finalName>    
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>

The <finalName> ..</finalName> does not make sense because your artifact is named based on your artifactId...furthermore an artifactId should be lowercase.

You have configured either Eclipse wrong or it's based on the wrong configuration.

The production code belongs to src/main/java/<packageName> and the according unit- or integration tests should be done into src/test/java/<packageName>.

For unit tests you should follow naming conventions like *Test.java what you already did. And for integration tests you should use *IT.java.

Furthermore remove the configuration for the resources:

 <resources>
<resource>
<directory>src/</directory>
<excludes>
<exclude>test/</exclude>
</excludes>
</resource>
</resources>

Because in your setup you've already used src/main/resources for test using src/test/resources.

Looking into the pom file you have given there are other strange things like mixing different junit-jupiter versions/parts.

So first remove the following from your pom file:

<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- Jupiter API for writing tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>

</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.8.2</version>
</dependency>

The easiest way to handle that is to use the junit bom file like the following:

  <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.8.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
..
</dependencyManagement>

Afterwards you have to define a single dependency in your pom like this:

 <dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
...
</dependencies>

Furthermore remove the whole setup/configuration of maven-surefire-plugin and replace with the following:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
</plugin>

The whole configuration/setup of the maven-compiler-plugin should simply look like this:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>

Also remove the whole configuration/setup of jacoco-maven-plugin and replace it with this:

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Remove the whole configuration/setup for copy-rename-maven-plugin.

Some words about usage of the dependencies:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>

Why using different versions of the same artifact? Remove commons-lang:commons-lang:2.6 and continue to work with org.apache.commons:commons-lang3:3.12.0 instead.

If you like to use sonar report you should execute the sonar-maven-plugin which is described here: https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-maven/

There are full working example https://github.com/khmarbaise/example-junitjupiter

One final strong recommendation is to define the all the plugins which are used during the build (see the example)..

Unable to run JUnit5 tests with Maven

As pointed out by other comments and answers I had residual JUnit4 dependencies due to test containers. I was able to fix the issue by explicitly setting JUnit5 as a dependency for maven surefire plugin like so :

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
</dependency>
</dependencies>
</plugin>


Related Topics



Leave a reply



Submit