DEV Community

Pavel Polívka
Pavel Polívka

Posted on • Originally published at ppolivka.com

7 2

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.

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay