Debugging Tests With Eclipse and Maven

Here are two techniques for debugging tests for Java Maven applications running Tomcat as you work in Eclipse.  There’s usually a problem with having two processes running — your test and then your production code (Tomcat) in two different processes.

These tests are integration style tests that run code against something running in a Tomcat container that is started by Maven (along with a database).   Throw your breakpoints in your test code AND your production code.  Also, during the debug process you might have to attach your source code for your production code; it’s not difficult.  You might have to restart the test after that . . .

Method One — Everything in One Process

You can run everything with a Maven configuration in eclipse, but it takes a bit longer for it to run. Setup a new Debug Configuration–>Maven Build in Eclipse.  Mess around with the setup, but this works for me:

Notice in the goals I have

-Dtest=IntegrationTest_1_Test -DfailIfNoTests=false clean install

If you have a more complex parent/submodule Maven pom setup like I do, you’ll need the failIfNoTests so that the entire thing can build before Tomcat kicks off. (Also depends on your project setup in the pom).

Method Two — Remote Process

This method has a few steps but works a bit faster.

1.  Start up your maven tomcat process in DEBUG mode from a command line.   You used to have to set a bunch of MAVEN_OPTS (and still can), but i Maven 3.0.5 and higher you can do this easily with a default port of 8000 by running mvnDebug instead of mvn:

start mvnDebug compile tomcat7:run

You get something like this as it waits:

2.  Create a DEBUG configuration for the remote port in Eclipse with port 8000, then start it with the Debug button:

3.  Run your test like you would in the IDE normally — no need to run it via Maven.  If you Debug your test — then you can throw breakpoints in the test and the code running in Tomcat.  If you just Run your test you will only hit the Tomcat breakpoints.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>