Maven - Running unit tests examples

[Updated: Mar 23, 2017, Created: Mar 14, 2017]

In this tutorial we will learn different ways to run unit tests in maven.

The 'test' phase

The 'test' phase is the part of maven default lifecycle (if not familiar with terms like lifecycle, plugin and goals then check out this tutorial). If we run the 'test' phase then all phases before test phase will pre-run (generally true for all phases). Let's use help:describe for 'test' phase (check out what is help:describe here)

 mvn help:describe -Dcmd=test
D:\example-projects\maven\maven-test-phase>mvn  help:describe -Dcmd=test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-test-phase 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ maven-test-phase ---
[INFO] 'test' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-jar-plugin:2.4:jar
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.4:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.558 s
[INFO] Finished at: 2017-03-14T14:00:55-05:00
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------

The line in above output test: org. apache. maven. plugins:maven-surefire-plugin:2. 12 . 4:test indicates that 'test' phase has the goal surefire:test. The surefire plugin uses a suitable unit testing framework. In our example we will add 'JUnit' dependency as testing framework.

Examples

In following examples, we are going to use multiple test classes to demonstrate the different ways to run 'test'.

Creating test classes

public class TestClass1 {

  @Test
  public void test1() {
      System.out.println("Running TestClass1#test1()");
  }

  @Test
  public void test2() {
      System.out.println("Running TestClass1#test2()");
  }
}
public class TestClass2 {

  @Test
  public void test3() {
      System.out.println("Running TestClass2#test3()");
  }

  @Test
  public void test4() {
      System.out.println("Running TestClass2#test4()");
  }
}
public class TestClass3 {

  @Test
  public void test5() {
      System.out.println("Running TestClass3#test5()");
  }

  @Test
  public void test6() {
      System.out.println("Running TestClass3#test6()");
  }

  @Test
  public void test7() {
      System.out.println("Running TestClass3#test7()");
  }
}

Running all test classes

mvn -q test

Output

D:\example-projects\maven\maven-test-phase> mvn -q test

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass1
Running TestClass1#test1()
Running TestClass1#test2()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.03 sec - in com.logicbig.example.TestClass1
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Running TestClass2#test4()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass2
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test6()
Running TestClass3#test7()
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass3

Results :

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

Note that, we are using -q for quiet output which only shows errors, test results and program output.

Running one test class only

mvn -q test -Dtest=TestClass3

Output

D:\example-projects\maven\maven-test-phase> mvn -q test -Dtest=TestClass3

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test6()
Running TestClass3#test7()
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec - in com.logicbig.example.TestClass3

Results :

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

Running set of test classes

mvn -q test -Dtest=TestClass3,TestClass1

Output

D:\example-projects\maven\maven-test-phase> mvn -q test -Dtest=TestClass3,TestClass1

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass1
Running TestClass1#test1()
Running TestClass1#test2()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec - in com.logicbig.example.TestClass1
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test6()
Running TestClass3#test7()
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass3

Results :

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

Using pattern to run classes

mvn -q test -Dtest=T*lass1

Output

D:\example-projects\maven\maven-test-phase> mvn -q test -Dtest=T*lass1

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass1
Running TestClass1#test1()
Running TestClass1#test2()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec - in com.logicbig.example.TestClass1

Results :

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

Running set of test methods

Running only one method:

mvn -q test -Dtest=TestClass2#test4

Output

D:\example-projects\maven\maven-test-phase> mvn -q test -Dtest=TestClass2#test4

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass2
Running TestClass2#test4()
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec - in com.logicbig.example.TestClass2

Results :

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

We can use just method name too:

mvn -q test -Dtest=#test4

Output

D:\example-projects\maven\maven-test-phase> mvn -q test -Dtest=#test4

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass1
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.043 sec - in com.logicbig.example.TestClass1
Running com.logicbig.example.TestClass2
Running TestClass2#test4()
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.logicbig.example.TestClass2
Running com.logicbig.example.TestClass3
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass3

Results :

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

Using pattern:

mvn -q  test -Dtest=T*tClass2#test3

Output

D:\example-projects\maven\maven-test-phase> mvn -q  test -Dtest=T*tClass2#test3

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 sec - in com.logicbig.example.TestClass2

Results :

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

Using pattern in method name:

mvn -q  test -Dtest=TestClass2#test*

Output

D:\example-projects\maven\maven-test-phase> mvn -q  test -Dtest=TestClass2#test*

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Running TestClass2#test4()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 sec - in com.logicbig.example.TestClass2

Results :

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

Running multiple methods from the same class (for this to work, we need surefire plugin least version 2.19. We have included that in the pom, see example project below. The maven version we are using is 3.3.9 (the latest stable version), which by default includes 2.12.4 version of surefire).

mvn -q  test -Dtest=TestClass3#test5+test7

Output

D:\example-projects\maven\maven-test-phase> mvn -q  test -Dtest=TestClass3#test5+test7

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test7()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec - in com.logicbig.example.TestClass3

Results :

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

Using multiple comma separated patterns:

mvn -q  test "-Dtest=*Class2#test3, TestClass3#*"

Output

D:\example-projects\maven\maven-test-phase> mvn -q  test "-Dtest=*Class2#test3, TestClass3#*"

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec - in com.logicbig.example.TestClass2
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test6()
Running TestClass3#test7()
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass3

Results :

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

Excluding tests

Excluding a test class:

mvn -q test "-Dtest=!TestClass1"

Output

D:\example-projects\maven\maven-test-phase> mvn -q test "-Dtest=!TestClass1"

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Running TestClass2#test4()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec - in com.logicbig.example.TestClass2
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test6()
Running TestClass3#test7()
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.logicbig.example.TestClass3

Results :

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

Excluding multiple test methods:

mvn -q test "-Dtest=!TestClass3#test6, !TestClass1#test1"

Output

D:\example-projects\maven\maven-test-phase> mvn -q test "-Dtest=!TestClass3#test6, !TestClass1#test1"

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.TestClass1
Running TestClass1#test2()
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 sec - in com.logicbig.example.TestClass1
Running com.logicbig.example.TestClass2
Running TestClass2#test3()
Running TestClass2#test4()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass2
Running com.logicbig.example.TestClass3
Running TestClass3#test5()
Running TestClass3#test7()
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in com.logicbig.example.TestClass3

Results :

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

Skipping all tests when running a post phase

mvn -q  package -DskipTests

Output

D:\example-projects\maven\maven-test-phase> mvn -q  package -DskipTests

Example Project

Dependencies and Technologies Used :

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck..
  • JDK 1.8
  • Maven 3.3.9

Maven Test Phase Examples Select All Download
  • maven-test-phase
    • src
      • main
      • test
        • java
          • com
            • logicbig
              • example

See Also