Monday, 27 October 2025

🐞 Debugging Maven Tests in IntelliJ Like a Pro

 When you’re writing tests in a Java project, sometimes a simple System.out.println() just isn’t enough. You want to step through the code, inspect variables, and understand exactly what’s going on.

If you’ve ever tried to debug your Maven tests using IntelliJ and found that your breakpoints don’t hit, this post is for you.

Let’s walk through how to properly attach a debugger to a Maven test process.

To debug your application or unit tests, you need to attach the debugger to the JVM running your tests, not the Maven build process.

Run this command instead:

mvn clean test -Dmaven.surefire.debug

You’ll see:

Listening for transport dt_socket at address: 5005

This means Maven’s Surefire Plugin (the one that runs your tests) has started a new JVM in debug mode, waiting for you to connect.

⚙️ Step 1: Set Up Remote Debug in IntelliJ

  1. Go to Run → Edit Configurations.

  2. Click + → Remote JVM Debug.

  3. Set:

    • Host: localhost

    • Port: 5005

    • Debugger mode: Attach to remote JVM

  4. Click OK to save.

🧠 Step 2: Attach and Run

Now go back to your terminal where you ran Maven. It’s currently waiting for a debugger.

In IntelliJ, click Debug ▶️ on the remote configuration you just created.

Once connected, IntelliJ will display:

Connected to the target VM, address: 'localhost:5005', transport: 'socket'

Now run your tests again if needed — and your breakpoints should hit right away 🎯

🚀 Conclusion

If your IntelliJ breakpoints weren’t working before — now you know why.
The magic lies in attaching to the Surefire (test) JVM, not the main Maven process.