Executing JUnit Testcases with Maven

Maven is a great tool for project dependency and build management. It can be used for running the Junit testcases for the project. In this post, I will show some simple but useful command examples to run testcases in various ways.

For demonstration, I have created a maven java project using following command:

mvn archetype:generate -DgroupId=com.howtodoinjava.junit -DartifactId=mavenJunitDemo
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

And then I created a test class as below in test folder.

package com.howtodoinjava.junit;

import org.junit.Test;

public class TestSurefire {

	@Test
	public void testcaseFirst()
	{
		System.out.println("First testcase executed");
	}

	@Test
	public void testcaseSecond()
	{
		System.out.println("Second testcase executed");
	}

	@Test
	public void testcaseThird()
	{
		System.out.println("Third testcase executed");
	}

	@Test
	public void otherTestcase()
	{
		System.out.println("Another testcase executed");
	}
}

Lets examine maven test command and see their outputs:

1) Run all testcases with command “mvn test” : This command run all testcases present inside test folder irrespective of any other criteria.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.howtodoinjava.junit.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec
Running com.howtodoinjava.junit.TestSurefire
Another testcase executed
First testcase executed
Third testcase executed
Second testcase executed
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 sec

Results :

Tests run: 5, Failures: 0, Errors: 0, Skipped: 0

2) Execute a particular test class only with “-Dtest=TestSurefire test” : This will execute all testcases inside test class TestSurefire.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.howtodoinjava.junit.TestSurefire
Another testcase executed
First testcase executed
Third testcase executed
Second testcase executed
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

You can use multiple test classes in command and even use wild cards in test class names to match a set of test classes. e.g. mvn -Dtest=TestSurefire,TestOth*Class test

3) Test only a certain testcase inside test class with “mvn -Dtest=TestSurefire#testcaseFirst test“: This command will execute only single test case method i.e. testcaseFirst().

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.howtodoinjava.junit.TestSurefire
First testcase executed
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

4) Test multiple test cases with wild card mapping e.g. “mvn -Dtest=TestSurefire#testcase* test“: This will help to run multiple testcases with similar names in one simple short command.

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.howtodoinjava.junit.TestSurefire
First testcase executed
Second testcase executed
Third testcase executed
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

Drop me a comment is something is not clear or i am missing anything.

Happy Learning !!

Leave a Reply

10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial