Last Updated:
How to Fix 'Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test' Test Failures in Maven Project
If you’ve ever run mvn test, mvn install, or mvn package on a Maven project, you’ve likely encountered the dreaded error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project your-project: There are test failures.
This error halts your build, preventing you from compiling, packaging, or deploying your project. At its core, the issue stems from the Maven Surefire Plugin—Maven’s built-in tool for executing unit tests (e.g., JUnit, TestNG). When the plugin fails to run tests successfully, it triggers this error.
In this blog, we’ll demystify this error, explore its root causes, and provide step-by-step solutions to get your build back on track. Whether you’re a beginner or an experienced developer, this guide will help you diagnose and fix the problem systematically.
Table of Contents#
Understanding the Error#
Before diving into fixes, let’s decode the error message:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project your-project: There are test failures.
org.apache.maven.plugins:maven-surefire-plugin: The plugin responsible for running unit tests in Maven.2.12: The version of the Surefire plugin being used (critical—we’ll revisit this!).test: The goal (task) being executed (runs unit tests).There are test failures: The high-level cause (tests didn’t pass, or the plugin couldn’t run them).
The error often includes a snippet like:
Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
This tells you how many tests failed (Failures) or errored out (Errors). The root cause is rarely the plugin itself—it’s usually a problem with your tests, configuration, or environment.
Common Causes and Fixes#
Let’s break down the most likely causes and how to fix them.
1. Actual Test Failures (Most Likely)#
Problem: Your unit tests are failing (e.g., assertion errors, unhandled exceptions). The Surefire plugin reports this as a build failure.
Diagnosis:
Check the Maven output immediately above the error message. You’ll see a stack trace or failure details for the problematic test. For example:
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.023 s <<< FAILURE! - in com.example.MyTest
[ERROR] testAddition(com.example.MyTest) Time elapsed: 0.007 s <<< FAILURE!
java.lang.AssertionError: Expected: 42, Actual: 43
Fix:
- Debug the failing test: Run the test in your IDE (e.g., IntelliJ, Eclipse) to isolate the issue.
- Fix the test logic: Correct assertions, handle exceptions, or fix bugs in the code under test.
- Re-run tests: Execute
mvn testto confirm the fix.
2. Outdated Surefire Plugin Version (2.12 is Ancient!)#
Problem: The error specifies maven-surefire-plugin:2.12—a very old version (released in 2012). Older versions lack support for modern test frameworks (JUnit 5), Java versions (Java 8+), and bug fixes.
Why this matters:
- Surefire 2.12 does not support JUnit 5 (JUnit Jupiter).
- It may have compatibility issues with Java 8+ features (lambdas, streams).
- Bugs in older versions can cause false positives (e.g., misreporting test successes/failures).
Fix: Update the Surefire Plugin
Update to the latest stable version (e.g., 3.5.6 as of 2026). Add this to your pom.xml:
<build>
<plugins>
<!-- Update Surefire Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.6</version> <!-- Check for latest at https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-plugin -->
</plugin>
</plugins>
</build> Run mvn clean test to apply the update.
3. Test Framework Compatibility Issues#
Problem: Mismatch between the Surefire plugin version and your test framework (JUnit 4, JUnit 5, TestNG).
Example 1: Using JUnit 5 (or JUnit 6) with Old Surefire#
Surefire 2.12 does not natively support JUnit 5 (JUnit Jupiter) or JUnit 6. If you're using JUnit dependencies like junit-jupiter-api, the plugin will fail to detect tests.
Fix for JUnit 5/6:
Update Surefire to 3.0.0+ (as shown above). Since Surefire 2.22.0+, the JUnit Platform provider is built-in—no additional provider dependency is needed.
For JUnit 5:
<dependencies>
<!-- JUnit 5 dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.14.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.6</version>
</plugin>
</plugins>
</build> For JUnit 6 (requires Java 17+):
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>6.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>6.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.6</version>
</plugin>
</plugins>
</build> Example 2: Using TestNG#
Older Surefire versions may require explicit TestNG configuration. Ensure your pom.xml includes:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.12.0</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.6</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile> <!-- If using TestNG XML suite -->
</suiteXmlFiles>
</configuration>
</plugin> 4. Misconfiguration in pom.xml#
Problem: Incorrect Surefire plugin settings in pom.xml (e.g., includes, excludes, forkCount, or argLine).
Common Misconfigurations:#
-
includes/excludes: Accidentally excluding all tests or including non-existent ones.
Example bad config:<configuration> <includes> <include>**/*NonExistentTest.java</include> <!-- No tests found! --> </includes> </configuration>Fix: Remove incorrect
includes/excludes, or use**/*Test.java(default) to include all test classes. -
forkCountandreuseForks:
forkCount=0(no forking) can cause classpath conflicts (e.g., static state leakage between tests).
Fix: SetforkCount=1(default) to fork a new JVM for tests:<configuration> <forkCount>1</forkCount> <reuseForks>true</reuseForks> </configuration> -
argLine(JVM Arguments):
Invalid JVM args (e.g., typos in memory settings) can crash the test JVM.
Example bad config:<argLine>-Xmx512m -invalidArg</argLine> <!-- "invalidArg" crashes the JVM -->Fix: Remove invalid args. Use
mvn test -X(debug mode) to see JVM crash logs.
5. Dependency Conflicts (e.g., JUnit Versions)#
Problem: Conflicting test library versions (e.g., mixing JUnit 4 and JUnit 5, or old TestNG jars).
Diagnosis:
Run mvn dependency:tree to list all dependencies. Look for conflicting versions (e.g., junit:junit:4.12 and org.junit.jupiter:junit-jupiter-api:6.1.2).
Fix:
- Exclude transitive dependencies: If a library pulls in an old JUnit 4 jar, exclude it:
<dependency> <groupId>com.example</groupId> <artifactId>some-library</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- Exclude JUnit 4 --> </exclusion> </exclusions> </dependency> - Use
dependencyManagementto enforce a single version:<dependencyManagement> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>6.1.2</version> </dependency> </dependencies> </dependencyManagement>
6. JVM Environment Issues (Memory, Java Version)#
Problem: The test JVM runs out of memory, or uses an incompatible Java version.
OutOfMemoryError (OOM)#
Tests may consume too much memory. The error log will show:
java.lang.OutOfMemoryError: Java heap space
Fix: Increase JVM memory with argLine:
<configuration>
<argLine>-Xmx1024m</argLine> <!-- 1GB heap -->
</configuration> Incompatible Java Version#
- Surefire 2.12 requires Java 5+ but may fail with Java 11+.
- JUnit 6 requires Java 17+.
- Your project may target Java 17, but Maven uses Java 8.
Check versions:
- Run
mvn -vto see Maven’s Java version:Apache Maven 3.9.16 Java version: 17.0.9, vendor: Eclipse Adoptium - Ensure it matches your project’s
source/targetinpom.xml:<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties>
7. Corrupted Surefire Reports or Cache#
Problem: Maven caches test results in target/surefire-reports/. Corrupted files here can cause false failures.
Fix: Clean the project to delete cached data:
mvn clean test General Troubleshooting Steps#
If the above fixes don’t work, try these systematic steps:
- Clean and Rebuild:
mvn clean test(wipestarget/and re-runs tests). - Run a Single Test: Isolate failures with
mvn test -Dtest=MyTest(replaceMyTestwith your test class). - Enable Debug Logs: Run
mvn test -X(adds-Xfor debug) to see detailed plugin execution logs. - Check IDE Tests: Run tests in your IDE (IntelliJ/Eclipse). If they pass there but fail in Maven, it’s a classpath/config issue.
- Verify Maven/Java Versions: Ensure Maven (3.9+) and Java (17+) are up-to-date.
- Run Tests Outside Maven: Use
java -cp target/test-classes:target/classes com.example.MyTestto bypass Maven and isolate JVM issues.
Conclusion#
The "Failed to execute goal maven-surefire-plugin:2.12:test" error is rarely caused by the plugin itself. Instead, it’s typically due to:
- Failing tests (fix the test logic).
- Outdated plugin versions (update to 3.x+).
- Compatibility issues with test frameworks (JUnit 5, JUnit 6, TestNG).
- Misconfiguration or dependency conflicts.
By systematically diagnosing the error log, updating tools, and verifying your setup, you’ll resolve the issue quickly. Remember: start with the simplest fixes (updating the plugin, cleaning the project, or fixing failing tests) before diving into complex configurations.