Last Updated : 23 Jul, 2025
When Maven fails to find JUnit tests to run, it can be frustrating and disrupt the development process. Several common issues can cause this problem, ranging from incorrect directory structures, missing dependencies, and to misconfigured plugins. One common issue developers face when using Maven is that it doesn't find and run JUnit tests.
Understanding Maven and JUnitMaven uses a Project Object Model file, pom.xml, to manage project configurations, dependencies, and plugins. JUnit is a widely used testing framework in Java for writing and running tests. For Maven, to successfully find and run JUnit tests, specific configurations must be correctly set in the pom.xml file.
Here's a high-level overview of why Maven might not find JUnit tests:We will 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
The above command generates a basic Maven project with the following project structure.
Maven Project Structure: Step 2: Adding DependenciesTo use JUnit, we need to add its dependency to the pom.xml file.
XML
<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>
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: Example Test Class
Create a sample test class in src/test/java/com/example
Java
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 compileOutput: Run the Tests:
mvn testOutput:
**/*Test.java
, **/Test*.java
, and **/*Tests.java
. Ensure your test classes follow these naming conventions.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.RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4