Friday, September 4, 2009

Debugging a single test through maven

Many times, your maven build fails because a unit test fails. SO, if want to debug the test you have 2 options

a) Run the test from eclipse> This is good for simple apps that do not need depend on 3rd party services . But, if your app relies on other 3rd party services starting up, chances are you have already configured maven to start those services when the unit tests are running. Now, if you have to run the same tests in eclipse, you will have to configure eclipse to start those services, and probably you will have to start the services from eclipse manually. Some developers choose this way, and there's nothing wrong with it, if it's easier for you to start everything from eclipse. However, remember that if you are working in a large team, other team members might change the configuration of the 3rd party servers in maven, and you are responsible for making sure the environment that you setup in eclipse reflects the environment in maven

b) Run the test from maven and remotely connect to maven from eclipse. If you want to run particulat test do like this
mvn -Dmaven.surefire.debug -Dtest= nameof test class test

This will run the test phase (along with all the other related phases), and just before it runs the surefire plugin, it will suspend execution and wait for debugger to connect on port 5005. Go to eclipse and srtup a remote debug configuration to port 5005 and start debugging. Your test will run, and stop at any breakpoints