Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

To run one Maven unit test class and leave other tests out of that run, use:

mvn test -Dtest=MyTest

Strictly speaking, -Dtest does not skip the test phase: it tells the Maven Surefire Plugin which tests to run. Use -DskipTests or -Dmaven.test.skip=true when you want to skip test execution instead.

What -Dtest does

-Dtest=MyTest sets Maven’s test user property. Surefire uses it to select matching test classes, usually by the class name without .java. The selection can include patterns, and the property overrides Surefire’s configured includes and excludes. It does not disable the test phase or avoid compiling test sources. See the Surefire test goal reference.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The selected test still needs to be compiled, compatible with the project’s configured test provider, and run by a lifecycle phase that reaches the relevant Surefire execution.

Run one test class

mvn test -Dtest=CalculatorTest

The same selector can be used with later lifecycle goals:

mvn verify -Dtest=CalculatorTest
mvn install -Dtest=CalculatorTest

Maven’s FAQ recommends the short test name for the basic case; Surefire also supports path and pattern forms when you need more specific matching. For example:

mvn test -Dtest=foo/MyTest.java
mvn test "-Dtest=**/MyTest.java"
mvn test "-Dtest=com.example.**.*Test"

See the official single-test examples and Maven FAQ.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run one method

mvn test -Dtest=CalculatorTest#addsTwoNumbers

You can use a method pattern, or select several methods in the same class:

mvn test "-Dtest=CalculatorTest#add*"
mvn test "-Dtest=CalculatorTest#testOne+testTwo"

Surefire documents this method-filter syntax for JUnit 4.x and TestNG. Do not assume it behaves identically with every JUnit 5 setup: class filtering is commonly supported, but method selection depends on the Surefire version and provider configuration. Check the project’s plugin configuration and provider behavior if a method selector matches nothing.

Run several classes, patterns, or all but one

Separate class names or patterns with commas:

mvn test -Dtest=CalculatorTest,UserServiceTest
mvn test "-Dtest=*ServiceTest"
mvn test "-Dtest=Calculator*"

To exclude a pattern, prefix it with !:

mvn test "-Dtest=*,!SlowTest"
mvn test "-Dtest=*Test,!IntegrationTest"

Quote selectors containing wildcards, exclamation marks, or other shell-sensitive characters. Shell behavior varies; without quotes, the shell may interpret part of the selector before Maven receives it.

-Dtest versus skipping tests

Option Executes tests? Compiles test sources? Use it to
-Dtest=MyTest Yes, matching tests only Yes Run a selected subset
-DskipTests No Yes Build without executing tests
-Dmaven.test.skip=true No No Skip both test execution and test compilation
mvn package -Dtest=MyTest
mvn package -DskipTests
mvn package -Dmaven.test.skip=true

Use -DskipTests if you want test code compiled but not run. Use -Dmaven.test.skip=true only when you also intend to bypass test compilation; otherwise, test-source compilation errors can go unnoticed. Maven documents the distinction in its FAQ and the Surefire guide to skipping tests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Unit tests and integration tests use different selectors

Surefire normally runs unit tests, selected with -Dtest. Integration tests managed by Maven Failsafe normally use -Dit.test and names such as *IT:

mvn test -Dtest=UserServiceTest
mvn verify -Dit.test=UserServiceIT

Failsafe’s default integration-test patterns include IT*.java, *IT.java, and *ITCase.java. Prefer verify for Failsafe runs: later lifecycle phases can perform cleanup after integration tests. See the Failsafe single-test example and Failsafe overview.

When Maven finds no matching test

A command such as mvn test -Dtest=DoesNotExistTest may fail or report that no specified tests ran. The exact behavior depends on the Surefire version and configuration, including failIfNoSpecifiedTests. Check these points:

  1. Spelling and name: Verify the class or method selector, including capitalization.
  2. Compilation: Confirm the test is present in the project’s test output; -Dtest does not search arbitrary source files.
  3. Plugin: If the test is an integration test, try Failsafe’s -Dit.test with verify.
  4. Discovery conventions: Without an explicit selector, Surefire’s common default class patterns are Test*.java, *Test.java, *Tests.java, and *TestCase.java. Projects can customize these patterns.
  5. Lifecycle and configuration: Check that the selected goal reaches the intended plugin execution, and look for profiles, custom testClassesDirectory settings, or exclusions that affect the run.

With JUnit 5, if a class selector works but a method selector does not, verify the Surefire version and provider rather than assuming the method syntax is universally supported. For tag-based selection, a provider-specific option may be more appropriate; for example, -Dgroups is not a universal JUnit 5 replacement. Consult the project’s JUnit Platform configuration and the Maven JUnit Platform documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Multi-module builds and multiple executions

A root-level command can pass the selector across a multi-module reactor:

mvn test -Dtest=MyTest

If the test belongs to one module, run Maven from that module or narrow the reactor, for example:

mvn -pl :module-name -am test -Dtest=MyTest

-pl selects the named project, while -am also builds required upstream projects. Reactor configuration varies, so a selector may be considered by more than one module; modules without a match can report no specified tests, depending on their plugin configuration.

Also check whether the POM defines multiple Surefire executions. The same test property may affect more than one execution, so the single-test selector may run in an unexpected context. Surefire’s single-test guidance notes this complication. TestNG suite XML configuration can also be overridden by -Dtest in supported configurations; do not assume the suite file remains authoritative.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick reference

Goal Command
One unit-test class mvn test -Dtest=MyTest
One method mvn test -Dtest=MyTest#myMethod
Several classes mvn test -Dtest=FirstTest,SecondTest
Wildcard with exclusion mvn test "-Dtest=*Test,!SlowTest"
One integration-test class mvn verify -Dit.test=MyIT
Skip execution, compile tests mvn package -DskipTests
Skip execution and test compilation mvn package -Dmaven.test.skip=true

A selected-test run is useful for quick iteration or debugging, but it provides evidence only about the tests that matched. Run the full suite before relying on a build for a merge or release.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.