To run an individual test in a Java project using Maven, you can use the mvn test
command and specify the test
parameter, followed by the fully qualified name of the test class.
For example, if you have a test class called MyTest
in the com.example
package, you can run the test using the following command:
mvn test -Dtest=com.example.MyTest
You can also specify multiple test classes by separating the fully qualified names with a comma. For example:
mvn test -Dtest=com.example.MyTest,com.example.AnotherTest
If you want to run a specific test method within a test class, you can use the #
symbol to specify the method name. For example:
mvn test -Dtest=com.example.MyTest#testMethod1
This will run only the testMethod1
method within the MyTest
class.
If you want to run all the test methods in a test class, you can use the *
symbol to indicate that all methods should be run. For example:
mvn test -Dtest=com.example.MyTest#*
This will run all the test methods in the MyTest
class.
You can also use the -Dtest.exclude
parameter to specify tests that should not be run. For example:
mvn test -Dtest=com.example.* -Dtest.exclude=com.example.MyTest#*
This will run all the test classes in the com.example
package except for the MyTest
class.