Why JUnit5 Tests are not running? [Solved]

So your JUnit 5 Jupiter Tests are not running in Maven ? no panic! This tutorial will provide all the checks you need to apply to your Maven project to unlock your JUnit 5 test execution.

JUnit 5 Tests are widely used to test the execution of Java Code. If you are new to them, the following article will guide you through a gentle introduction to them: JUnit 5 Made Easy

Now let’s come to the issue, that is, you are running the test goal but your JUnit 5 tests are not running! Let’s solve it!

why junit 5 tests are not running with maven

Missing JUnit 5 engine dependency

The most common cause of missing the execution of JUnit 5 tests is that you are missing the junit-jupiter-engine dependency in your Maven project:

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

As a matter of fact, the junit-jupiter-api alone is not sufficient so make sure you are including also junit-jupiter-engine.

Update your Maven surfire plugin

Another common cause of issue is that you might be using an outdated version (older than version 2.2.2) of Maven maven-surefire-plugin. Make sure you use a recent one like the following one:

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

Behind the hoods, this might be caused by an older Maven installation which ships with a version of Maven surfire plugin. For example, if you are running Maven 3.6.0, it will include version 2.22.0 of the Maven Surefire Plugin.

Therefore, if you are running JUnit 5 Tests consistently the simplest solution is to update Maven version!

Missing JUnit annotations

Check if your test class or test methods are missing the necessary JUnit 5 annotations like @Test, @BeforeEach, @AfterEach, etc.

Incorrect naming Conventions

Ensure that your test methods follow the JUnit 5 naming convention. Here is the naming convention of the Maven Surefire Plugin to trigger Tests::

  • Starts or ends with the string: Test.
  • Ends with the string: Tests.
  • Ends with the string: TestCase.

For example:

@Test
@DisplayName("Test addition functionality")
void testAddition() {
        Assertions.assertEquals(15, value + 5);
}

Incorrect Class location

Confirm that your test classes are placed in the correct directory structure (src/test/java for Maven projects by default) and follow the correct package hierarchy.

Here is a correct location for a Test Class:

tree src
src
├── main
│   └── java
│       └── com
│           └── example
│               └── project
│                   └── Calculator.java
└── test
    └── java
        └── com
            └── example
                └── project
                    └── CalculatorTests.java

Incorrect Test Signature

Finally, make sure your test methods are public, void, and take no arguments. Otherwise, JUnit 5 may not recognize them as test methods.

Conclusion

In conclusion, ensuring proper execution of JUnit 5 tests in Maven involves several critical steps. By confirming the correct dependencies, configuring plugins, adhering to naming conventions, and verifying the test class and method placements, you can rectify issues preventing the execution of JUnit 5 tests. Remember to align your Maven version with the compatible Surefire and Failsafe plugin versions to seamlessly run your JUnit 5 tests within the Maven environment. Following these steps will enable a smooth and efficient execution of your JUnit 5 tests using Maven, allowing you to leverage its powerful testing capabilities within your projects.

Found the article helpful? if so please follow us on Socials