Last Updated: February 25, 2016
· tmarwen

Debug a test case under a mavenized project

Sometimes is shows up as a hard task to debug a simple testcase 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 an ultimate method as you can run your tests using the maven surfire plugin attached for remote debug on some port.
Just run the test phase with maven.surefire.debug system property:

mvn test -Dmaven.surefire.debug

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

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"

This will attach the tell the jvm process to listen on 8000 port.