Maven does not find JUnit tests to run Maven does not find JUnit tests to run java java

Maven does not find JUnit tests to run


By default Maven uses the following naming conventions when looking for tests to run:

If your test class doesn't follow these conventions you should rename it or configure Maven Surefire Plugin to use another pattern for test classes.


I also found that the unit test code should put under the src/test/java folder, it can not be recognized as test class if you put it under the main folder.eg.

Wrong

/my_program/src/main/java/NotTest.java

Right

/my_program/src/test/java/MyTest.java


UPDATE:

Like @scottyseus said in the comments, starting from Maven Surefire 2.22.0 the following is sufficient:

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

When using JUnit 5, i ran into the same problem. Maven Surefire needs a plugin to run JUnit 5 tests. Add this to our pom.xml:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-surefire-plugin</artifactId>    <version>2.21.0</version>    <dependencies>        <dependency>            <groupId>org.junit.platform</groupId>            <artifactId>junit-platform-surefire-provider</artifactId>            <version>1.2.0-M1</version>        </dependency>        <dependency>            <groupId>org.junit.jupiter</groupId>            <artifactId>junit-jupiter-engine</artifactId>            <version>5.2.0-M1</version>        </dependency>    </dependencies></plugin>

Source:https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven

UPDATE 2021

The junit-platform-surefire-provider, which was originally developed by the JUnit team, was deprecated in JUnit Platform 1.3 and discontinued in 1.4. Please use Maven Surefire’s native support instead.