mvn surefire:test: Run Unit Tests Directly
The surefire:test goal runs unit tests; -Dtest selects which tests, and it is the goal bound to the test phase so mvn test invokes it.
Where the test phase runs everything before it, calling surefire:test directly runs just the unit tests, and -Dtest targets a single class or method.
What it does
The maven-surefire-plugin's test goal runs the unit tests of the project against the test classpath and writes reports to target/surefire-reports. It is bound to the test phase, so mvn test runs it. Calling mvn surefire:test runs it without re-triggering earlier phases (assuming classes are already compiled).
Common usage
mvn test
mvn test -Dtest=UserServiceTest
mvn test -Dtest='UserServiceTest#shouldCreate*'
mvn -B test -Dtest='*ServiceTest' -DfailIfNoTests=falseFlags
| Flag | What it does |
|---|---|
| -Dtest=<pattern> | Run only matching classes/methods (# selects a method) |
| -DskipTests | Compile but do not run tests |
| -Dmaven.test.skip=true | Skip compiling and running tests |
| -DfailIfNoTests=false | Do not fail when -Dtest matches nothing |
| -Dsurefire.rerunFailingTestsCount=N | Retry failing tests N times |
In CI
Use -Dtest to re-run a single flaky class quickly without the whole suite. Run with -B for clean logs and cache ~/.m2. Always archive target/surefire-reports so failures are inspectable after the runner is torn down.
Common errors in CI
"There are test failures. Please refer to ... target/surefire-reports" points to the XML/txt reports. "No tests were executed!" with a non-zero exit means -Dtest matched nothing; add -DfailIfNoTests=false or fix the pattern. "The forked VM terminated without properly saying goodbye. VM crash or System.exit called?" is usually an OOM or a test calling System.exit; raise the forked heap via argLine.