Maven does not find JUnit tests to run
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with Java, Maven is a popular choice for managing project dependencies and automating builds. An integral part of ensuring that your Java applications are robust and reliable is systematic testing, which often involves using libraries such as JUnit. However, a common issue many developers encounter is that Maven does not find or run JUnit tests. There are several reasons why this could happen, and understanding these can help in effectively configuring Maven to recognize and execute your tests.
Understanding Maven and JUnit Integration
Maven uses a standard directory layout requiring source files and test files to be in specific directories. Typically, your main source files reside in src/main/java, and your test files should be in src/test/java. Maven, through its Surefire Plugin, then automatically compiles and runs tests from the test directory.
Reasons Maven Might Not Find JUnit Tests
- Incorrect Directory Structure: As mentioned, Maven expects test files in
src/test/java. Placing test files elsewhere can lead the tests to be ignored. - Naming Conventions: Maven’s Surefire Plugin, which is responsible for running JUnit tests, looks for test files that adhere to specific naming patterns. By default, these are
**/Test*.java,**/*Test.java,**/*Tests.java, and**/*TestCase.java. - Dependency Issues: The lack of appropriate JUnit dependencies in your
pom.xmlfile can also cause Maven to skip running tests. Ensure that you have the correct version of JUnit added. - Configuration Errors in
pom.xml: Misconfigurations or typos in thepom.xmlcan prevent the Surefire Plugin from executing the tests.
Exploring Solutions
1. Verify Directory Structure
Ensure your test classes are in the correct directory (src/test/java). Here’s a typical structure:
2. Confirm Naming Conventions
Check the names of your test classes to ensure they end with Test or start with Test (following CamelCase naming conventions). For instance, UserServiceTest.java or TestUserService.java.
3. Check JUnit Dependency in pom.xml
Your pom.xml should include JUnit as a dependency. Here is an example for JUnit 5:
4. Inspect pom.xml Configuration
Ensure that the Surefire Plugin is correctly configured in your pom.xml. Here’s an example of a typical configuration:
Additional Troubleshooting Steps
- Run Maven with
-Xflag to get debug output, which can provide insights into what Maven is doing behind the scenes and why it might be skipping tests. - Update Surefire Plugin: Ensure you are using a recent version of the Maven Surefire Plugin that supports the version of JUnit you are using.
- Check for Plugin Conflicts: Other plugins in your
pom.xmlmight interfere with Surefire. Temporarily commenting out other plugins can help determine the cause.
Summary Table
| Issue | Checking Point | Recommended Action |
| Directory Structure | src/test/java exists | Move tests to the correct directory |
| Naming Conventions | Test class names | Rename class files to end with Test or begin with Test |
| Dependencies | JUnit dependency in pom.xml | Add or update JUnit dependency |
| Configuration Errors | Configuration of Surefire in pom.xml | Ensure correct plugin configuration |
| Debugging | Maven command line | Use -X for detailed debug information |
| Plugin Conflicts | Other plugins in pom.xml | Comment out other plugins to rule out conflicts |
By systematically addressing these points, you can resolve most issues regarding Maven not finding or running your JUnit tests, ensuring your build process is both robust and efficient.
Related reading
- Maven Failed to read artifact descriptor
- Maven is not using Java 11 error message Fatal error compiling invalid target release 11
- Maven is not working in Java 8 when Javadoc tags are incomplete
- Maven module using spring-boot
- Meaning of delta or epsilon argument of assertEquals for double values
- Measure execution time for a Java method
- Maven package/install without test (skip tests)
- Maven Plugin not found in IntelliJ IDE

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.