Wednesday, January 15, 2014

Remote debug unit test run by MAVEN

Once in a while, you have this unit test that run perfectly within your IDEA / Eclipse ide, but fails when executed by maven.

If it happens, you may want to debug your unit test when it fails.
In order to remote debug it , when executed by maven, you can do the following:

SureFire Method

You can achieve it in another way by adding -Dmaven.surefire.debug to your mvn command. the debug port is 5005.
If you need to cancel the the fork of your surefire plugin , add -DforkCount=0.

All is explained nicely in: Maven Surefire Plugin - Debugging Tests

Thanks to GuyH for the tip. Didn't try it yet but it should work.

My old fashion method

Add the "remote debug" JVM argument to your maven execution:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044


In addition, In addition, be sure that your maven surefire-plugin, responsible of running your unit test, doesn't execute your unit test(s) in fork mode but within its own jvm, so you can add breakpoint on the unit test itself. 

         <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-surefire-plugin</artifactId>                                <configuration>                                        <forkMode>always</forkMode>        <<== this must be deleted or on false                                </configuration>                <inherited>true</inherited>            </plugin>


Then, once your maven is running, attach your IDE tool (eclipse/idea or whatever) to your JVM (here, in our above example, attach it to port 1044, on the host where your maven is running) 

If maven is running too quickly, you can use the suspend mode (suspend=y) , so your maven JVM when start until you attach your IDE to it:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044


No comments: