Skip to content

/commands/maven/mvn-run-single-test

mvn test -Dtest=MyTest: run a single test class or method with Maven

The commands on this page have not been executed by our verification runner yet.updated
tool
Maven
versions
Maven 3.x, maven-surefire-plugin 3.x
os
any

mvn test -Dtest=OrderServiceTest runs only the test class OrderServiceTest, and mvn test -Dtest=OrderServiceTest#createsOrder runs only one method of it. The test property is read by the Maven Surefire plugin, which runs unit tests in the test phase; every other test class is skipped. Patterns with *, several entries separated by commas and ! exclusions are allowed.

Usage

bash
mvn test -Dtest=OrderServiceTest                     # one class
mvn test -Dtest=OrderServiceTest#createsOrder        # one method
mvn test -Dtest='OrderServiceTest#creates*'          # methods matching a pattern
mvn test -Dtest='Order*Test,PaymentTest'             # several classes
mvn test -Dtest='!SlowTest'                          # everything except one class
mvn -pl service -am test -Dtest=OrderServiceTest -Dsurefire.failIfNoSpecifiedTests=false

Quote patterns that contain *, ! or # so the shell does not expand or interpret them. In PowerShell, quote the whole -Dtest=... argument, because PowerShell otherwise splits it at the dot.

Patterns

-Dtest valueRuns
OrderServiceTestThe class OrderServiceTest in any package
com/acme/order/OrderServiceTestThat class in that package (packages written with slashes)
OrderServiceTest#createsOrderOne method
OrderServiceTest#creates*Methods whose name starts with creates
OrderServiceTest#createsOrder+cancelsOrderTwo methods of one class
Order*Every class whose name starts with Order
**/order/**/*TestClasses in any order package
!SlowTestAll tests except SlowTest

Pitfalls

  • Multi-module builds fail on modules without a match. Surefire fails a module when -Dtest matches nothing there; -Dsurefire.failIfNoSpecifiedTests=false turns that off. Older Surefire 2.x versions used -DfailIfNoTests=false.
  • Old Surefire versions. Method patterns with # and + and combined patterns are not supported by very old Surefire releases, and projects that do not pin the plugin get whatever version their Maven release defines. Pin maven-surefire-plugin in pluginManagement for predictable behaviour.
  • JUnit 5 tests not found. If tests are never found with or without -Dtest, the JUnit Platform dependency (junit-jupiter) is missing or Surefire is too old to use it.
  • Skipping all tests is a different option: see skipTests vs maven.test.skip.
  • Integration tests (*IT classes run by Failsafe) need -Dit.test=... and the verify phase.

How to confirm it worked

The Surefire summary at the end of the build, Tests run: N, Failures: 0, Errors: 0, Skipped: 0, gives the number of tests that ran; with a single method it is 1. Surefire also writes one report per class under target/surefire-reports/, so only the selected classes appear there after a clean build (mvn clean test -Dtest=...).

FAQ

Why does Maven fail with No tests matching pattern were executed?

The pattern matched no test in at least one module. In a multi-module build add -Dsurefire.failIfNoSpecifiedTests=false so modules without a matching test do not fail, or limit the build to one module with -pl.

How do I run one integration test?

Integration tests run by the Failsafe plugin use -Dit.test instead, for example mvn verify -Dit.test=OrderIT. -Dtest only selects tests for Surefire in the test phase.

How do I run a single test in a multi-module project?

Select the module and build what it depends on: mvn -pl service -am test -Dtest=OrderServiceTest -Dsurefire.failIfNoSpecifiedTests=false.

Sources