← Debugging Guides

阅读中文版本

Maven BUILD FAILURE — surefire Test Mojo Failed

Read a Maven surefire failure to separate compilation errors, assertion failures, and environment issues, then rerun or target the failing test.

intermediatedebuggingjavamaventesting

Symptoms

  • mvn test ends with "BUILD FAILURE" and "[ERROR] Failed to execute goal ... surefire ..."
  • Tests pass individually but fail in the full run
  • The report shows failures in one class only

Possible causes

  • A real assertion failure in one test aborts the build
  • The forked JVM ran out of memory or hit a timeout
  • A test depends on another test order or on shared static state

Diagnosis steps

Read the surefire summary and reports

mvn -q test 2>&1 | grep -E "Tests run|FAIL|ERROR" | head -20

Interpretation

The summary distinguishes errors from failures and points to the reports directory.

Rerun only the failing test

mvn -Dtest=OrderServiceTest#rejectsNegativeTotal test

Interpretation

Reproducing a single test isolates logic from order or environment effects.

Check for a crashed fork

grep -rn "The forked VM terminated\|Corrupted STDOUT" target/surefire-reports

Interpretation

That message means surefire lost the JVM; raise forkCount memory or use reuseForks=false.

Root cause

The surefire plugin forks a JVM to run tests and fails the build on any failure or error. Failures fall into three classes: assertion mismatches, JVM crashes (fork error), and environment/order dependencies, each reported distinctly.

Fix

Fix the real assertion, isolate order-dependent tests (fresh fixtures, no shared static state), and adjust surefire forking and memory. Use -Dmaven.test.failure.ignore only for diagnostics, never in CI.

Risk note

Skipping tests with -DskipTests masks regressions; skipping compilation differs from skipping execution — know which you need.

Verify the fix

Run the full test phase again and confirm BUILD SUCCESS with the expected Tests run count; ensure per-class reports show zero failures and errors.

Symptoms

The error “Maven BUILD FAILURE — surefire Test Mojo Failed” points at a specific failure mode. This guide documents the symptoms you can observe, the commands that isolate the cause, the root cause, the fix, and the checks that prove the fix works.

Observed symptoms include:

  • mvn test ends with “BUILD FAILURE” and “[ERROR] Failed to execute goal … surefire …”
  • Tests pass individually but fail in the full run
  • The report shows failures in one class only

Reproduce and Diagnose

Reproduce the failure in isolation before changing any code. Work through the checks below in order; each one narrows the search space.

Step 1: Read the surefire summary and reports

mvn -q test 2>&1 | grep -E "Tests run|FAIL|ERROR" | head -20

The summary distinguishes errors from failures and points to the reports directory.

Step 2: Rerun only the failing test

mvn -Dtest=OrderServiceTest#rejectsNegativeTotal test

Reproducing a single test isolates logic from order or environment effects.

Step 3: Check for a crashed fork

grep -rn "The forked VM terminated\|Corrupted STDOUT" target/surefire-reports

That message means surefire lost the JVM; raise forkCount memory or use reuseForks=false.

Likely contributors, in order of frequency:

  • A real assertion failure in one test aborts the build
  • The forked JVM ran out of memory or hit a timeout
  • A test depends on another test order or on shared static state

Root Cause

The surefire plugin forks a JVM to run tests and fails the build on any failure or error. Failures fall into three classes: assertion mismatches, JVM crashes (fork error), and environment/order dependencies, each reported distinctly.

The Fix

Fix the real assertion, isolate order-dependent tests (fresh fixtures, no shared static state), and adjust surefire forking and memory. Use -Dmaven.test.failure.ignore only for diagnostics, never in CI.

Watch out: Skipping tests with -DskipTests masks regressions; skipping compilation differs from skipping execution — know which you need.

<!-- pom.xml: isolate each test class in its own JVM -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.2.5</version>
  <configuration>
    <reuseForks>false</reuseForks>
    <argLine>-Xmx1g</argLine>
  </configuration>
</plugin>

# run a single test
mvn -Dtest=OrderServiceTest#rejectsNegativeTotal test

Verify and Prevent

Run the full test phase again and confirm BUILD SUCCESS with the expected Tests run count; ensure per-class reports show zero failures and errors.

Once verified, add a regression test that reproduces the original symptom so the failure cannot silently return.

This class of failure is usually environmental rather than pure logic, so capture the exact command, the environment variables, and the working directory in your bug report. Reproduce it in the smallest possible script, then widen the scope only once the minimal case is understood. A short written checklist for this failure mode saves hours the next time it appears.