How to run unit test with Maven
To run unit test via Maven, issue this command :
mvn testThis 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 :
mvn testExample 2
To run single test (TestApp1), issue this command :
mvn -Dtest=TestApp1 test
Example 3
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.

how to use the Maven in Spring MVC 3.0, Hibernate 3.0 project? what’s the main need to use Maven in string mvc + hibernate based project?
I have tried to run single unit test case using below command but it is not working
mvn -Dtest=TestApp2 test
Is it works for only Maven 2.0 ? I am using Maven 3.0.4 .
Appreciate your help.
Thanks ,
Uma.
Thanks a lot. The example with the syntax saved my time..
I would like to know if i can import Maven 3 projects and run goals in the community version of intellij or do I need to use Maven 2 for this?
I would appreciate some advise on the most recommended way to use Maven within Intellij. Thanks for all your insightful tutorials.
Sorry, never use Intellij. Since Intellij is a paid product, why not just drop an email to their support team?
Was a paid product. Most Eclipse featuered were copy cats of Idea:
http://www.jetbrains.com/idea/free_java_ide.html I found two good links to my question btw: http://blogs.jetbrains.com/idea/2008/03/opening-maven-projects-is-easy-as-pie/
and http://wiki.jetbrains.net/intellij/Creating_and_importing_Maven_projects
Thanks Mkyong
Thanks for your correction, and nice links sharing :)
According to the assertEquals() API (http://www.junit.org/apidocs/org/junit/Assert.html) the first argument is an expected value, the second is an actual one.
The example has incorrect arguments order.