Marketplace for Content, Tests and Assessment
 
 
Home > Programming / tutorials > Debug Maven tests running inside eclipse

Debug Maven tests running inside eclipse

For those of us who have been running JUnit tests inside eclipse , one of the biggest feature available to us was the debug ability of the JUnit tests.
When we started working on Maven tests , the first questions which came to mind was , How am I going to debug it ?
I started with running the tests class by selecting the class in eclipse and right –> Run as Junit Test.

But this does not work. There are some variables which are being injected through pom.xml and tons of other dependency bugs which would be thrown.

So here is how to run your units tests in debug mode while running through mvn command.  There are two steps to this. You will have to first set up a remote launcher, similar to the way you might have done for remote server.

Then you will run the tests from command prompt or

Create Remote Java Application Debug Configurations

Create a debug configuration let us say “Maven Debug”

Run > Debug… and then right click on Remote Java Application and chose New Launch Configuration. Save the this new configuration as “Maven Debug”.  On the Connect tab click the Browse… button and select the “Maven Debug” project. Make sure that the Connection Properties > Port is 8000 and host is localhost . (This matches the address value set on the MAVEN_OPTS command line in the mvndebug.bat file from above. mvndebug.bat file can be found under bin in your installation directory)

This has set up your Maven remote launch.

Running maven Tests

On the command prompt go to the project where your test class resides. for eg c:/development/maven/skillMaven

Run mvnDebug -DforkMode=never test -Dtest=ShiftListFormActionTest

You can run all the test cases or only selected test from here.

If you want to run all the tests

mvnDebug -DforkMode=never test

After you run this command , you will see

Preparing to Execute Maven in Debug Mode
Listening for transport dt_socket at address: 8000

Now go to eclipse and click on Maven Launch configuration created above .

This will launch the debugger in Maven.

And now you will the action happening in the command prompt and Vola..test hits the debugger

Why are we using -DforkMode=never ? By default, Maven runs your tests in a separate (“forked”) process.
You can force Maven not to fork tests by configuring the forkMode configuration parameter

mvnDebug -DforkMode=never test

Some reference sites which I had used and you might find helpful are

http://docs.codehaus.org/display/MAVENUSER/Dealing+with+Eclipse-based+IDE

http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html

Categories: Programming / tutorials Tags: ,
  1. Ela
    March 11th, 2010 at 20:36 | #1

    wow! cool. nice to know.

  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes