'How to enable debug on my JUnit through Gradle test task

I get into trouble while I try to run my JUnit test through Gradle test task. While I run the test in eclipse directly with Run As -> JUnit test, everything is ok, the test succeeds. But through test task, test always fails. Probably some trouble with the encoding of my resource txt file. So I would like to enable debug while I am launching the test with Gradle

in build.gradle, my test task now looks like:

test {
    tasks.withType(Compile) {
        options.encoding = 'UTF-8'
    }
}

So what should I do to enable debug? I run Gradle tasks from Gradle panel in Eclipse, not from the console. Thanks!



Solution 1:[1]

As explained under 23.12. Test in the Gradle User Guide, executing gradle test -Dtest.single=MyTestClass -Dtest.debug will suspend the test JVM upon start, and allows to connect an external debugger (such as the Eclipse debugger) on port 5005.

Solution 2:[2]

Putting this here as --debug-jvm did not work for me, I was able to do this by setting:

 org.gradle.daemon=true
 org.gradle.jvmargs=... -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=10999

in

 ~/.gradle/gradle.properties

But when I connect with eclipse debugger for the project none of the breakpoints I've set compile/trigger... I am connected via the debugger, I can see action in the Debug view, whenever I run gradle test from command line, like new threads starting/stopping, but can't get breakpoints to trigger, trying to resolve this now...

Fyi to stop deamon run gradle --stop

Other solution

Leaving above as reference, this worked for triggering break points in tests, I turned off deamon as I could not get it to work properly:

Using directions from this article: http://blogs.steeplesoft.com/posts/2013/gradle-tip-attaching-a-debugger.html

test {        
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug',
            '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
    }
}

Executed via gradle test -DDEBUG=true

Solution when using the JUnit Platform Gradle plugin

The solution above won't work when using org.junit.platform.gradle.plugin.

Instead it should be replaced by:

junitPlatformTest {        
    if (System.getProperty('DEBUG', 'false') == 'true') {
        jvmArgs '-Xdebug',
            '-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10999'
    }
}

Solution 3:[3]

I'm on 4.6 (gradle) and I'm able to debug my tests when I have this in my build.gradle file:

test {
    debug true
}

Link - https://docs.gradle.org/4.6/userguide/userguide_single.html#sec:java_test

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Niederwieser
Solution 2 Vic Seedoubleyew
Solution 3 MartyIX