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
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:
-Xdebugargument just enables the ability of debugging of the java process
-Xrunjdwpoption configures the Java process to work as a debugging server and allows other clients (debuggers) to connect.
suspend=yoption tells the Java process to wait until the debugger connects, with
suspend=noption you can attach at any moment – the java process will continue the execution without the debugger.
transport=dt_socketparameter 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)
server=ymakes 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
address=8000parameter sets the port, at which the Java process will listen for the debugger
-Djava.compiler=NONEparameter 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).