August 17, 2014

Debugging a unit test in a Maven project

How to debug a unit test in Maven (Java) project

Djebel Ressas viewed from Mont Boukornine


Sometimes it shows up as a hard task to debug a simple test case method as there should be some rules to meet, dependencies to inject, or configurations to initialize and that may be out of the current test class scope.

If your are using Maven as your build tool, there is a straightforward method through running your test fixtures using the maven-surfire-plugin attached for remote debug on some port.

Run the test phase in debug mode

Using a terminal shell, you can run the test phase with maven.surefire.debug system property:

mvn test -Dmaven.surefire.debug
This will attach the debugger to listen on port 5005 (used by default), so just configure your favorite IDE to attach to that port and set up a break point.

Change the default debug port

You can even change the port number using:

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

Using above command will run the Maven test phase while attaching the debugger to listne on port 8000.

Tagged with java, maven, test, debug, inspect

© 2022 Marouane Trabelsi. All rights reserved.