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:

To debug tests the following argument should be used: --debug-jvm

For example: gradle test --debug-jvm
Gradle will suspend execution right before running tests and wait for debugger connection on port 5005.

For executing only specific tests see https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern

Solution 2:

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 3:

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'
    }
}