Maven – Debug Test

In Debug, Maven, Test on 17/03/2011 by pier0w

So if you happen to program in Intellij IDEA like my self you may have come across a situation where your tests pass within your IDE but fail when running mvn clean install. Generally this is because you did something silly in one of your tests that is now infecting all the following tests.

Now it’s all well and good knowing you’ve done something wrong, but it’s not so helpful if you don’t actually know WHAT that something is. So now you’ll be thinking “I KNOW! I’ll run mvn clean install within Intellij IDEA in debug mode then step through them there tests. I SO SMART!”. Then with your maven build profile all configured within Intellij and your break points all precisely set you click on run debug and… the tests run to the end without a pause in site.

Now is when you’re wondering what the duce just happened. Well it turns out that maven runs it’s test in forked processes, this means that you may be happily debugging the maven process but you have absolutely no connection to the forked processes that are running the tests. So what to do?

The first, easiest and maybe even best thing to do is add the -DforkMode=never parameter to your mvn clean install command to become…

mvn clean install -DforkMode=never

Who’d of thought?

In regards to your profile, put it in the Goals text field.

This will stop maven forking the tests so they will stay within the maven process you are actually debugging. So problem solved.

Though, what if you want to debug some tests that are running on another server? Well that is actually very simple as well. Simple kick the tests off with the -Dmaven.surefire.debug parameter included within your command. The maven tests will now pause at the beginning and expose a debug socket on port 5005 where you can then connect your favourite IDE to carry out the debugging. This works weather the tests are forked or not.

mvn clean install -Dmaven.surefire.debug

And that’s it, now you know how to debug your maven tests no matter where they are.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s