How to enable debug on my JUnit through Gradle test task
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 Simple name pattern
For additional options see Debugging when running tests.
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'
}
}
Solution 4
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
Related videos on Youtube
mefi
Updated on November 10, 2021Comments
-
mefi over 2 years
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!
-
Peter Niederwieser over 10 yearsThis configuration doesn't affect the
test
task but allCompile
tasks. Hence it shouldn't be nested insidetest { .. }
. -
mefi over 10 yearsIn some test classes (and also in regular java classes), there are some constant strings with diacritics, so I need to run test with UTF-8 encoding. While I used this, tests who failed before now runs ok. If you say this is not ok, could you help me how to correct this (While i keep diacritics string in java classes)? Use this encoding only on tests?
-
Peter Niederwieser over 10 yearsIf you want to set the encoding for all
JavaCompile
tasks, usetasks.withType(JavaCompile) { options.encoding = 'UTF-8' }
. If you only want to set it for test compilation, usecompileTestJava { options.encoding = 'UTF-8' }
. (Compile
has been deprecated and is now calledJavaCompile
.)
-
-
mefi over 10 yearsThank you, is it possible to use it directly from Eclipse?
-
Peter Niederwieser over 10 yearsDepends on what exactly you mean by that. From what I remember, the Eclipse Gradle tooling does support custom command line parameters, so it should be possible to start the build from Eclipse. Then you'll have to fire up the Eclipse debugger.
-
mefi over 10 yearsFine, I`ll try it. Thank you very much!
-
RuntimeException almost 9 yearsThanks Peter. In case where I am unable to use port 5005, how do I disable fork of test JVM. Or change the port number to something else other than 5005?
-
Robin Green over 8 yearsFirstly, that's usually called an "option", not an "attribute". Secondly, it doesn't work for me to just prepend it to the command line arguments - it's necessary to append it.
-
niken almost 8 yearsThis did not work for me at all... I am able to connect with a debugger in deamon mode but none of my breakpoints compile/trigger in eclipse... this feature is kind of weak sauce
-
Sergey almost 8 yearsSince you enabled daemon, there are two JVMs running. You are connecting to the wrong one.
-
niken almost 8 years2 jvms listening on 1 port? How can that be? I don't think I was connecting to wrong jvm, deamon was registering all calls to gradle, from eclipse or command line client... Like I said I saw it "doing stuff" in debugger whenever I ran any gradle command
-
nilskp over 7 years@Nik, the reason it's not working is that Eclipse does it's own build in the
bin/
folder, which is where it attaches the breakpoints. Gradle also builds tobuild/classes/
, which is the classfiles it runs. Thus not working. -
Thomas W over 6 yearsIf you have difficulty with build-tool options, you can capture & reproduce the process launch and configure that to attach your debugger. literatejava.com/engineering/how-to-debug-tests-run-externally
-
Vic Seedoubleyew over 6 yearsDoesn't seem to work at all for me using using Gradle 4.4.1, it doesn't seem to notice it at all
-
Vic Seedoubleyew over 6 yearsThis doesn't work when using org.junit.platform.gradle.plugin, in which case the second part of that answer should be used : stackoverflow.com/a/38210920/2873507
-
Vic Seedoubleyew over 6 yearsThis doesn't work when using org.junit.platform.gradle.plugin, in which case the second part of that answer should be used : stackoverflow.com/a/38210920/2873507
-
romeara over 4 yearsNewer versions of Gradle have a specific DSL, independent of test framework, documented in the current Gradle version user guide
-
Roland Puntaier over 2 yearsThis will listen on port 5005. You can then
jdb -sourcepath <...> -attach 5005
andstop at com.<...>.<TestClass>:<line number>
.test {debug true}
is top level in build.gradle.