Fork me on GitHub

Debugging Tests

Sometimes you need to debug the tests exactly as Maven ran them. Here's how!

Forked Tests

By default, Maven runs your tests in a separate ("forked") process. You can use the maven.surefire.debug property to debug your forked tests remotely, like this:

mvn -Dmaven.surefire.debug test

The tests will automatically pause and await a remote debugger on port 5005. You can then attach to the running tests using Eclipse. You can setup a "Remote Java Application" launch configuration via the menu command "Run" > "Open Debug Dialog..."

If you need to configure a different port, you may pass a more detailed value. For example, the command below will use port 8000 instead of port 5005.

mvn -Dmaven.surefire.debug="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000" test

Non-forked Tests

You can force Maven not to fork tests by setting the configuration parameter forkCount to 0.

mvn -DforkCount=0 test

Then all you need to do is debug Maven itself. Maven ships with a mvnDebug shell script that you can use to launch Maven with convenient debugging options:

mvnDebug -DforkCount=0 test

Then you can attach Eclipse to Maven itself, which may be easier/more convenient than debugging the forked executable.