DEV Community

Pavel Polívka
Pavel Polívka

Posted on • Originally published at ppolivka.com

Debugging Maven tests in IntelliJ IDEA

We all know it. You write the test, it works when you click the Run button in IntelliJ but it fails when run with mvn clean test.

It can be all sorts of issues, related to jar packaging, etc... You add the breakpoint into your test, but how to convince Maven's Surefire plugin to debug mode?
How to use awesome power set of IntelliJ debugging tools?

Remote debug to the rescue.

IntelliJ IDEA

Setting up remote debug for your Tomcats, etc can be very cumbersome, but doing it for Maven Surefire plugin tests is actually very easy.

In IntelliJ we create new run Configuration.

IntelliJ IDEA Run Configuration

We can just add new one and leave the defaults in.

Maven

Now we execute the maven run in debug mode.

mvn test -Dmaven.surefire.debug
Enter fullscreen mode Exit fullscreen mode

This will start the tests in debug mode. Read the terminal text and when there is line that the debug mode started, start the IntelliJ run config.
It will attach itself to the executing test, and you can use all the tools IntelliJ provides.

If you want to execute just one concrete test, you can do it like so:

mvn test -Dtest=TestClass.java#testMethod -Dmaven.surefire.debug
Enter fullscreen mode Exit fullscreen mode

If you like this article you can follow me on Twitter.

Discussion (0)