How to debug maven tests

The problem usually occurs when you don’t have a possibility to run tests by your IDE (where there is a built in support of debugger), but you have to debug the failing test remotely. The command we should use should be:

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

The property maven.surefire.debug of the Surefire Plugin configures the behaviour of the debugger during the execution the test phase. The above command use the Java Debug Wire Protocol (JDWP) to connect to the maven java process. Then we can use the jdb to obtain the connection to the java process and control the debugging. Here are the used parameters:

  • The-Xdebug argument just enables the ability of debugging of the java process
  • The-Xrunjdwp option configures the Java process to work as a debugging server and allows other clients (debuggers) to connect.
  • The suspend=y option tells the Java process to wait until the debugger connects, with suspend=n option you can attach at any moment – the java process will continue the execution without the debugger.
  • The transport=dt_socket parameter specifies the transport protocol used between processes. There are two possible options:
    • dt_socket – socket interface
    • dt_shmem  – shared memory (in this case the application and the debugger must run on the same machine)
  • The server=y makes the Java process listening on the specified address. If set to server=n, then the Java process will try to attach the debugger at the specified address parameter
  • The address=8000 parameter sets the port, at which the Java process will listen for the debugger
  • The -Djava.compiler=NONE parameter disables the JIT compiler

As we see, we have quite big control over the configuration of the debugging process, but usually the example command is sufficient – we can use it as it is.

We can use it not only for the debugging of the maven tests, but also for other applications, for example running on the application servers (like Tomcat).

Leave a Reply

Your email address will not be published. Required fields are marked *