Table of Contents
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
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package org.arpit.java2blog; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; public class AssertSameTest { /* * Examples for each overloaded methods of assertSame */ //public static void assertSame(Object expected, Object actual) @Test public void test1(){ String str1="India"; String str2="India"; Assertions.assertSame(str1,str2); } //public static void assertSame(Object expected, Object actual, String message) @Test public void test2(){ String str1=new String("India"); String str2=new String("India"); Assertions.assertSame(str1,str2,"str1 and str2 do not refer to same object"); } //public static void assertSame(Object expected, Object actual, Supplier<String> messageSupplier) @Test public void test3(){ String str1=new String("India"); String str2=str1; Assertions.assertSame(str1,str2,() -> "str1 and str2 do not refer to same object"); } } |
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.
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.
Above command will execute test3 testcase of AssertSameTest class.
Run multiple test methods from a test class
Above command will execute test1 and test2 test method of AssertSameTest class.
You can also use pattern matching to execute the testcases.
It will run all the testcases which starts from test
in AssertSameTest.
That’s all about how to run single JUnit test in maven.