JavaRocky
JavaRocky

Reputation: 19945

In IntelliJ, how do i debug a maven test goal?

Using intellij and maven pom files, how do i debug tests run inside the maven test goal?

When i run them directly in the code, it complains something about profiles missing, which i've ticked inside intellij's Maven Projects.

Upvotes: 105

Views: 73084

Answers (8)

dualed
dualed

Reputation: 10512

There is at least now documentation by JetBrains at https://www.jetbrains.com/help/idea/work-with-tests-in-maven.html#debug_maven

That is:

  1. In intellij, create a remote-jvm debug configuration, select the service (important in multi-module project), type socket, host localhost and set the port to something other than 5005 (replace below) and just leave the rest to default
  2. run
    mvn -Dmaven.surefire.debug="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:{port}" -Dtest=YourTestClassName test
    
  3. Since we're passing suspend=y, the surefire process will pause when starting the test VM, at this point you start the remote debug "build" in IntelliJ.

Further notes

The JetBrains guide says to run in IDE and that does work, but I found it easier to just run in a separate shell.

You really want to restrict to a single test class, otherwise you'll have to reconnect for every single class.

Disabling forking had no impact at all for me, at least with Quarkus tests.

Upvotes: 1

Alik Titelman
Alik Titelman

Reputation: 51

mvn clean verify -DforkCount=0 when using surefire plugin

Upvotes: 4

Hover Ruan
Hover Ruan

Reputation: 3495

http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html

  • When surefire plugin version < 2.14: use -DforkMode=never
  • When surefire plugin version >= 2.14: use -DforkCount=0

In IDEA, open your run/debug configuration, in Runner tab, add fork options -DforkCount=0

enter image description here

Upvotes: 235

David DRM
David DRM

Reputation: 21

The solution from Colin Hebert is not working to me neither. But fortunelly I found an easy way to debug the test by doing right click on the green triangle appears next to the test method:

Java test class example

I hope thats helps you!

Upvotes: 1

DesertBlade
DesertBlade

Reputation: 391

-DforkMode=never doesn't work anymore, it is now deprecated in SureFire.

Use -DforkCount=0 instead when using surefire plugin 2.14+.

Upvotes: 29

Robin Loxley
Robin Loxley

Reputation: 824

The question has been answered. But just to share my own experience. The selected answer did not solve my problem. My code has multiple modules.

foolshat's reply did bring valuable insight to my problem.

I have two solutions, 1. Using your IDEA, by adding a VM option -DforkMode=never; Must run it with debug mode. 2. Set up a remote debugging, specifying the socket and in this case forkMode is not necessary.

It is just a summary for what I have been through.

Upvotes: 10

Colin Hebert
Colin Hebert

Reputation: 93197

What about a right click on your goal and "Debug [your goal]" (in your case the test goal)?

debug goal

Upvotes: 27

Henryk Konsek
Henryk Konsek

Reputation: 9168

I execute tests with the following options:

mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=6666 -Xnoagent -Djava.compiler=NONE" test

... and then connect to Maven with remote debugger.

Upvotes: 36

Related Questions