Run Specific Tests with Maven
· One min read
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
-plselects projects from the reactor.-amalso builds required upstream modules.-DskipTestscompiles tests but skips execution.-Dmaven.test.skip=trueskips 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.