Skip to main content

Run Specific Tests with Maven

· One min read
Apache Wangye
Software developer and technical writer

Maven Surefire accepts the -Dtest property for selecting unit tests.

Run all tests:

mvn test

Run one class:

mvn -Dtest=UserServiceTest test

Run one method:

mvn -Dtest=UserServiceTest#shouldCreateUser test

Run several methods or wildcard patterns:

mvn -Dtest='UserServiceTest#shouldCreate*' test
mvn -Dtest='UserServiceTest,OrderServiceTest' test

For a multi-module project, select a module and build required dependencies:

mvn -pl service-module -am -Dtest=UserServiceTest test
  • -pl selects projects from the reactor.
  • -am also builds required upstream modules.
  • -DskipTests compiles tests but skips execution.
  • -Dmaven.test.skip=true skips both test compilation and execution.

Integration tests commonly run through Maven Failsafe during integration-test and verify, so use the plugin's include properties and lifecycle rather than assuming mvn test runs them.

Page views: --

Total views -- · Visitors --