Surefire is not picking up Junit 5 tests
Your surefire ticket to enabling Surefire to identify JUnit 5 tests is to update your Maven pom.xml
with JUnit Jupiter dependencies and setup the Surefire plugin with JUnit 5 compatibility:
Jump on board with @Test
from org.junit.jupiter.api
, not JUnit 4's org.junit
. This simple switch powers up Surefire's JUnit 5 compatibility.
Spot and solve integration hurdles
Even with a spot-on build setup, you may face unexpected roadblocks. If Surefire's not picking up your JUnit 5 tests, let's walk through how to detect and disentangle them:
-
Double-checking compatibility: Ensure you're running compatible versions of Maven, JUnit, and Surefire.
-
Ensure the Java version in your
pom.xml
aligns with JUnit 5 needs—Java 8 or higher: -
Test method names should follow conventions, or Surefire might give them a cold shoulder.
-
Adjust with
@TestInstance
annotation, if your tests depend on JUnit 5's changed default test instance lifecycle. -
Parameterized tests need the right
junit-jupiter-params
dependency to integrate smoothly. -
Community-contributed Maven project examples on GitHub often provide 'aha' moments.
-
Upgrading to a Surefire plugin version that natively understands JUnit 5 could be your silver bullet.
-
The
junit-platform-surefire-provider
has hit retirement for JUnit 5.5.1 and beyond, so exclude it.
The gist of the pom.xml configuration
Understanding our pom.xml
configuration sets the stage for a smooth JUnit 5 adoption:
JUnit Jupiter dependencies
Our testing train needs the right rails, which are JUnit Jupiter dependencies:
junit-jupiter-api
: The Test API that we write against.junit-jupiter-engine
: The powerhouse, or engine, for our test execution.
Set the scope
to test
to limit their scope to during the test phase.
Maven Surefire Plugin configuration
The Maven Surefire plugin is the driver that perfectly maneuvers our JUnit 5 tests. Aim for version 2.22.0 or above for native JUnit 5 support, without additional configurations.
Upgrading the test setup
Fresh versions, Fresh capabilities
Check for latest stable releases on Maven Central:
- Use the latest stable version of JUnit Jupiter.
- Keep your Maven Surefire Plugin updated.
Rolling with Maven profiles and properties
To tailor your build process, Maven's profiles can set different properties for various environments:
- Use profiles to include JUnit libraries under certain conditions.
- Use properties to maintain consistent builds across different setups.
Peer into your repository
Sometimes, a peek into your version control system can reveal test case clues, especially if they've stopped being recognized.
Was this article helpful?