How to run a single test in Maven

How to run a single Class Test with Maven

Open a terminal and from the Maven project directory (which contains a valid pom.xml file) you can execute the following command:

mvn -Dtest=ExampleTest test

where ExampleTest is a class which contains a Test in it.

How to run a single Test from a Class with Maven

On the other hand, if you want to run just a single test method contained in a Class, then you can execute the following:

mvn -Dtest=ExampleTest#testInit test

where testInit is a Test method contained in the class ExampleTest

How to Run Tests that Match a Pattern

You can also run selected tests in your test class which match a pattern. For example, the command below will run all tests in a test class that start with “testInit”.

mvn -Dtest=ExampleTest#testInit* test