Explain Codes LogoExplain Codes Logo

Surefire is not picking up Junit 5 tests

java
interview-preparation
best-practices
maven
Alex KataevbyAlex Kataev·Jan 23, 2025
TLDR

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:

<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.2</version> <!-- Latest as of writing, feel free to update --> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.8.2</version> <!-- Nothing but the latest for our engine --> <scope>test</scope> </dependency> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> <!-- Surefire Plugin’s sweet spot for JUnit 5 compatibility --> </plugin>

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:

  1. Double-checking compatibility: Ensure you're running compatible versions of Maven, JUnit, and Surefire.

  2. Ensure the Java version in your pom.xml aligns with JUnit 5 needs—Java 8 or higher:

    <properties> <maven.compiler.source>1.8</maven.compiler.source> <!-- I should be 8, like my favourite spider arachnid --> <maven.compiler.target>1.8</maven.compiler.target> </properties>
  3. Test method names should follow conventions, or Surefire might give them a cold shoulder.

  4. Adjust with @TestInstance annotation, if your tests depend on JUnit 5's changed default test instance lifecycle.

  5. Parameterized tests need the right junit-jupiter-params dependency to integrate smoothly.

  6. Community-contributed Maven project examples on GitHub often provide 'aha' moments.

  7. Upgrading to a Surefire plugin version that natively understands JUnit 5 could be your silver bullet.

  8. 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.