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 :

mvn test

Example 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.
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: alkaline water benefits()

  • Pingback: water ionizers()

  • Pingback: laan penge nu og her()

  • Pingback: parking()

  • Pingback: parking()

  • Pingback: fue()

  • Pingback: tvpackages.net()

  • Pingback: tvpackages.net()

  • Pingback: TV options for restaurants()

  • Pingback: best online casinos()

  • Pingback: gloving()

  • Pingback: spring bulbs planting()

  • Pingback: kangen water()

  • Pingback: kangen water machine()

  • Pingback: alkaline()

  • Pingback: water ionizers()

  • Pingback: tv online, online tv()

  • Pingback: watch tv show episodes()

  • Pingback: watch movies online()

  • Pingback: Blue Coaster33()

  • Vishal N

    Earlier my projects were having Junit test in source code only as I am using ant.

    Now we have shifted to Maven. Can you tell me how can i run the junit test which are not test test dir but in main dir?

  • Ibo

    it is really wonderfull. but i have a problem
    [INFO] Reactor Summary:
    [INFO]
    [INFO] TestParent …………………………………. SUCCESS [0.012s]
    [INFO] Test1 ……………………………………… SUCCESS [4.910s]
    [INFO] Test2 ……………………………………… SUCCESS [0.571s]

    cd/\TestParent> mvn test
    result Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    but i have
    more test functions,
    test1 class i have more than 3 funtions tests
    and Test class2 I hve 6 Tests,
    when I do like this , cd parent/cd test1/ mvn test.
    only 1 Function run, other 5 not,
    when i do with spesific class like this

    mvn -Dtest=test3 test ( 6 funtions)
    it is ok,., it runs all functions

    can u explain more please ;
    please

  • Mukesh

    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?

  • Uma

    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.

  • JAY

    Thanks a lot. The example with the syntax saved my time..

  • ScalaGirl

    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.

  • Dmytro

    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.