Maven - Debugging JUnit tests

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

All IDEs provide facility to run and debug JUnit tests. It is, however, not always possible to run application from IDE and we have only option to run it from command line using mvn command. There we can run tests in debug mode using following command:

mvn -Dmaven.surefire.debug test

Example

Let's create a very simple project with a JUnit test.

D:\LogicBig\example-projects\maven\maven-debug-test
public class TestClass {

  @Test
  public void test() {
     String str = "a test string";
      System.out.println(str);
  }
}

Run the test from project root

D:\LogicBig\example-projects\maven\maven-debug-test>mvn -Dmaven.surefire.debug test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-debug-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-debug-test ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ maven-debug-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-debug-test ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\LogicBig\example-projects\maven\maven-debug-test\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ maven-debug-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-debug-test ---
[INFO] Surefire report directory: D:\LogicBig\example-projects\maven\maven-debug-test\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Listening for transport dt_socket at address: 5005

The last line indicates that maven has automatically paused and will wait for a remote debugger to connect at port 5005.

In the next step, we have to setup our IDE (where our project source is) to debug the tests remotely.

Following video shows how to do that. We are using Intellij IDE. Setting up remote debugger from IDE is just like setting up the debugger for remote server application.


If we want to change the port then we can use following command:

mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" test

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 Debug Tests Example Select All Download
  • maven-debug-test
    • src
      • main
      • test
        • java
          • com
            • logicbig
              • example

See Also