Explain Codes LogoExplain Codes Logo

Maven does not find JUnit tests to run

java
maven
junit
test-execution
Anton ShumikhinbyAnton Shumikhin·Feb 28, 2025
TLDR

Quickly get Maven to recognize your JUnit tests. Name your test files following the (Test* or *Test).java, such as PaymentTest.java or TestInvoice.java. Place them in the src/test/java directory. Also, confirm the **/maven-surefire-plugin** in your pom.xml. It should be configured to include those tests:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Test.java</include> <!-- Duran Duran "*Test" album sold out --> <include>**/Test*.java</include> <!-- The Beatles album "Test*" is classic --> </includes> </configuration> </plugin>

This explicit inclusion makes sure Maven discovers your tests and runs during the test phase.

Aligning with JUnit version

Ensure your Maven Surefire Plugin matches your JUnit version. For JUnit 5, Maven Surefire needs to be at least 2.22.0. Here are dependencies for JUnit 5:

<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <!-- the cool kid in town --> <version>1.3.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <!-- the engine that powers it all --> <version>5.6.2</version> <scope>test</scope> </dependency>

Old JUnit 4 or its grandpa? Include junit-vintage-engine as a test scope dependency:

<dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <!-- the good ol' vintage jazz --> <version>5.6.2</version> <scope>test</scope> </dependency>

Maven lifecycle check-up

Check if Maven runs your tests at the right lifecycle phases. Believe me, test-compile and test phases are like gym for Maven, where your tests should sweat. Fire up mvn -X test to generate debug output. This treasure map can guide you to any hidden hitch preventing the execution of your tests.

Practical guidelines for seamless Maven & JUnit Integration

Resolving Maven Artifact Conflicts

Inspect pom.xml like a hawk for potential conflicts among dependencies that might lead to Maven turning a blind eye on your JUnit tests. Remember, overlapping versions can be like bumpy roads for Maven.

Configuring Classpath for Test Execution

Ensure your classpath setup didn't miss the bus. A correct classpath includes all necessary libraries and frameworks — they are your VIP passengers.

Troubleshooting with Maven Output

Maven can sometimes act mysterious. Keep an eye for errors or warnings in its output. Solving these can be like having the key to the locked door.

Get the Annotations Right

Make sure your test classes wear the right badges (JUnit annotations like @Test). Or else, Maven might walk past them in a crowded room. Also, don’t forget Java is case-sensitive. Incorrect case could be the reason why Maven couldn't find your tests.