Maven may fail to find JUnit tests due to configuration issues or improper project setup. This problem usually occurs when test files, dependencies, or plugins are not correctly defined in the project.
- Tests not placed in src/test/java directory
- Missing or incorrect JUnit dependency in POM
- Improper Surefire plugin configuration.
Setting Up a Maven Project
Step 1: Creating a Maven Project
Create a Maven project using an IDE like IntelliJ IDEA, Eclipse, or from the command line.
mvn archetype: generate -DgroupId=com.example -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Here:
- -DgroupId=com.example: Defines the base package name
- -DartifactId=myapp: Sets the project name
- -DarchetypeArtifactId=maven-archetype-quickstart: Uses the standard Maven template
- -DinteractiveMode=false: Runs the command without prompting for user input
The above command generates a basic Maven project with the following project structure.
Maven Project Structure:

Step 2: Adding Dependencies
To use JUnit, we need to add its dependency to the pom.xml file.
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Step 3: Configuring Plugins
Maven Surefire plugin is responsible for running the tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
Step 4: Create Test Class
Create a sample test class in src/test/java/com/example
package com.example;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AppTest {
@Test
public void testApp() {
assertEquals(1, 1);
}
}
Maven Commands to Compile and Run Tests
To compile and run your tests, we can use the following Maven commands:
Compile the Project:
mvn compile
Output:

Run the Tests:
mvn test
Output:

- Ensure JUnit Version Compatibility: Make sure you are using compatible versions of JUnit and Maven Surefire Plugin. If using JUnit 5, the dependencies and plugin configurations differ.
- Check Test Naming Conventions: Maven Surefire Plugin by default looks for test classes with names matching
**/*Test.java,**/Test*.java, and**/*Tests.java. Ensure your test classes follow these naming conventions. - Check Test Output: After running
mvn test, check the output to ensure tests are being detected and executed. Look for lines indicating the number of tests run and their status.