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 !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

10 thoughts on “Executing JUnit Testcases with Maven”

  1. hiii all

    plzzz help

    test cases are not running but showing a running message

    I m trying to test my test case by using mvn test on command line but
    ####………running com.obelxy.business.impl.DialPlanServiceImplTest……##
    only dis message is showing on cmd and test cases are not running.what can i do to run them???

    Reply
  2. Hi All,

    Please help…

    Getting error when running the following command…..

    mvn archetype:generate DgroupId=com.univoxcommunity -Dartifa
    ctId=uni -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    ERROR:
    Microsoft Windows [Version 6.1.7600]

    Copyright (c) 2009 Microsoft Corporation. All rights reserved.
    C:Windowssystem32>mvn archetype:generate DgroupId=com.univoxcommunity -Dartifa

    ctId=uni -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=fals

    e

    [INFO] Scanning for projects…

    [INFO] ————————————————————————

    [INFO] BUILD FAILURE

    [INFO] ————————————————————————

    [INFO] Total time: 0.270 s

    [INFO] Finished at: 2014-10-23T10:21:12+05:30

    [INFO] Final Memory: 3M/15M

    [INFO] ————————————————————————

    [ERROR] The goal you specified requires a project to execute but there is no POM

    in this directory (C:Windowssystem32). Please verify you invoked Maven from t

    he correct directory. -> [Help 1]

    [ERROR]

    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit

    ch.

    [ERROR] Re-run Maven using the -X switch to enable full debug logging.

    [ERROR]

    [ERROR] For more information about the errors and possible solutions, please rea

    d the following articles:

    [ERROR] [Help 1] https://cwiki.apache.org//confluence/display/MAVEN/MissingProject

    Exception

    C:Windowssystem32>

    Reply
  3. I was getting below exception when I run the given maven command.
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateLifecycleMappings(DefaultLifecycleExecutionPlanCalcula
    tor.java:228)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateMojoExecutions(DefaultLifecycleExecutionPlanCalculator
    .java:199)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.
    java:118)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleExecutionPlanCalculator.calculateExecutionPlan(DefaultLifecycleExecutionPlanCalculator.
    java:135)

    Solution……………………………….

    Please update the POM file.

    junit
    junit
    4.11
    test

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.