mvn test: Usage, Options & Common CI Errors
Run unit tests through Surefire - and read the failure summary it prints.
test is the Maven lifecycle phase that runs the project’s unit tests using the maven-surefire-plugin. It first runs the test-compile phase, then executes matching test classes and fails the build if any test fails.
What it does
test compiles test sources to target/test-classes and runs them with Surefire. By default it includes classes matching *Test, Test*, *Tests, and *TestCase. Results are written to target/surefire-reports.
Common usage
mvn test
mvn test -Dtest=UserServiceTest # one class
mvn test -Dtest=UserServiceTest#createsUser # one method
mvn test -DskipTests # build but skip running tests
mvn test -Dmaven.test.failure.ignore=true # report failures, do not fail buildCommon error in CI (and the fix)
Symptom: "No tests were executed!" with Surefire, or a strict run failing because zero tests matched. Cause: test classes do not match the default naming patterns, or -Dtest filtered everything out. Fix: rename to *Test/*Tests, or configure the <includes> in maven-surefire-plugin; to allow an empty run, set -Dsurefire.failIfNoSpecifiedTests=false when using a -Dtest filter.