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.

About the Author

author image
mkyong
Founder of Mkyong.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

Leave a Reply

avatar
newest oldest most voted
Dmytro
Guest
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.

Anna
Guest
Anna

Thank you very much!

evgmoskalenko
Guest
evgmoskalenko

Could you tell me please. How I can run “one test method” if my test class (TestApp1.java) have more one test methods?

This command “mvn -Dtest=TestApp1 test” execute test class

Thanks.. :-)

Kumar Abdasu
Guest
Kumar Abdasu

When i trying run the single test by using maven, getting this error

Ex: mvn -Dtest=Reports test

where, Reports is class

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
12:test (default-test) on project TestNGReports: No tests were executed! (Set –
DfailIfNoTests=false to ignore this error.) -> [Help 1]

Gopinadh
Guest
Gopinadh

actually i don’t know any thing about this maven ,i know only Ant and how can i run my project from ant to maven,please me help me to out.

Ryan
Guest
Ryan

Just to note, mvn test will check tests on “test.java” package, by default.

Vishal N
Guest
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
Guest
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
Guest
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
Guest
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
Guest
JAY

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

ScalaGirl
Guest
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.