How To Run Unit Test With Maven
To run unit test via Maven, issue this command :
mvn test
This will run the entire unit tests in your project.
Case Study
Create two unit tests and run it via Maven. See a simple Java class for testing :
package com.mkyong.core; public class App { public static void main(String[] args) { System.out.println(getHelloWorld()); } public static String getHelloWorld() { return "Hello World"; } public static String getHelloWorld2() { return "Hello World 2"; } }
Unit Test 1
Unit test for
getHelloWorld()
method.package com.mkyong.core; import junit.framework.Assert; import org.junit.Test; public class TestApp1 { @Test public void testPrintHelloWorld() { Assert.assertEquals(App.getHelloWorld(), "Hello World"); } }
Unit Test 2
Unit test for
getHelloWorld2()
method.package com.mkyong.core; import junit.framework.Assert; import org.junit.Test; public class TestApp2 { @Test public void testPrintHelloWorld2() { Assert.assertEquals(App.getHelloWorld2(), "Hello World 2"); } }
Run Unit Test
See below examples to run unit test with Maven.
Example 1
To run the entire unit test (TestApp1 and TestApp2), issue this command :
To run the entire unit test (TestApp1 and TestApp2), issue this command :
mvn test
Example 2
To run single test (TestApp1), issue this command :
To run single test (TestApp1), issue this command :
mvn -Dtest=TestApp1 test
Example 3
To run single test (TestApp2), issue this command :
To run single test (TestApp2), issue this command :
mvn -Dtest=TestApp2 test
Note
For more “mvn test” examples, refer to this maven-test plugin documentation.
For more “mvn test” examples, refer to this maven-test plugin documentation.
No comments:
Post a Comment