Run single test in maven

In this post, we will see how to run single test in maven.

If you want to execute all the Junit testcases in maven. You can run it below command.

Run all testcase

mvn test

You need to go to the project location which contains pom.xml and execute above command.

Run all testcases from a test class

You can use -Dtest=$JunitClassName to execute all the test cases in particular Junit class.
For example:
Let’s say you have below test class AssertSameTest.

Here we have used assertSame() method of Assertions class to see if two strings refer to same object.
You want to run only AssertSameTest in maven.

You can simply use below command.

mvn test -Dtest=AssertSameTest

Run specific test method from a test class

Let’s say you want to execute only special test case in AssertSameTest class, you can run as below.

mvn test -Dtest=AssertSameTest#test3

Above command will execute test3 testcase of AssertSameTest class.

Run multiple test methods from a test class

mvn test -Dtest=AssertSameTest#test1+test2

Above command will execute test1 and test2 test method of AssertSameTest class.

You can also use pattern matching to execute the testcases.

mvn test -Dtest=AssertSameTest#test*

It will run all the testcases which starts from test in AssertSameTest.

That’s all about how to run single JUnit test in maven.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *