When running a gradle unit test from command line, how can I get a debugger? - gradle

I'm running unit tests from the gradle command line (because running them from IntelliJ was causing problems for some reason.)
So I'd like to turn on a debug port for remote debugging from the unit test. How can I do that? Is there a way to send the -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y into the unit test itself? I'm using Gradle daemon mode so I'm not sure how that all works and can't find any documentation about it.

Actually it turned out that there's no need to set the jvmArgs manually. Instead you just need to invoke (docs):
gradle someTestTask --debug-jvm
and it will stop execution and wait for debugger connection. Now when this process hangs, go to IntelliJ, Eclipse (or other IDE) and set up an remote debugging configuration (remember to set source module - red arrow):
After it's set, run the debugger and you're done. Breakpoints may be set in IntelliJ directly.
Previous answer below:
test {
jvmArgs '-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y'
}

Related

Intellij cannot debug cucumber + serenity test run

I got java cucumber + serenity project in Intellij
I configured the maven run configure:
Working directory: xxxxx
Command line: clean verify "xxxxx"
The run configure works fine before, I can use it run test as well as fire up the debug mode.
However, one of sudden, I cannot run the debug mode anymore. Even though I clicked the debugged button (green little bug) it never stops at the break point.
It still stops if I added break point to java.lang Object class, but it never stops at any break point from actual test methods, very weird...any one can help? Thanks
Surefire starts a new jvm to run tests (a fork). You are debugging the jvm that starts the second jvm, not the jvm that runs the tests.
Either change the fork mode or attach a debugger to the forked jvm.
http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html

Trying to debug an application that starts up via an "exec" gradle task

I have a gradle task (type - exec). In this task I start a WildFly server using a standart bat file (standalone.bat). The war file is already in deployments beforehand, so the server starts up and everything works fine.
The problem appears when I need to debug the actual application on the server. Running the gradle task in a debug mode doesn't help, as it can only debug the actual gradle task itself and not the application that it starts.
I know, there is a solution to this problem using an additional configuration that would allow me to debug a remote java application. However, the goal for me right now is to move eveything onto gradle tasks and not have any configurations set up in my IDE.
I need to be able to run a gradle exec task that would start up my server and than to debug whatever application it deploys. Is there a way to do this? Thank you in advance.
Note: I've never used Wildfly
Ultimately you'll want to edit standalone.bat so that you include an additional arguments to the java.exe invocation. Take a look inside standalone.bat many applications include a line commented out that you can uncomment to enable debugging.
Eg sample argument to add to the java.exe call
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
You may wish to take a copy of standalone.bat which you call standalone-debug.bat which you invoke via gradle

Running gradle test tasks in Microsoft App-V5.1 virtualized environment

I'm very new to App-V, which is evaluated in my office.
I have a Selenium test suite written in JUnit5 and can launch it as gradle test task using gradle-wrapper. My final goal is to run this on App-V5.1 virtualized environment, similar as this question.
As followed the link which was mentioned the answer, I tried to launch cmd.exe within the App-V environment, and it seeded works. Then, I tried to do this:
./gradlew --no-daemon clean test
Then the testClasses phase works perfectly, but in the test phase, I got an error like:
Could not write standard input into: Gradle Worker 1.
java.io.IOException: The pipe is being closed
...
(Sorry I couldn't show you the actual error log due to security reason, but it is similar to this question.)
May I wrong something? What's the right way to launch a gradle test in App-V env?
Have you tried launching cmd.exe from within the virtual bubble? I find the best way to do this is to create a shortcut to cmd.exe during sequencing and use this to troubleshoot.
If your process works within the bubble, the solution may be as simple as allowing Local Interaction. Have a read here about that.

How to debug a Gradle build.gradle file (in a debugger, with breakpoints)?

Is there a tool that will allow me to set breakpoints in a build.gradle file and step through tasks in a debugger?
Note: I believe that I'm asking a different question than similar stackoverflow questions about debugging Gradle plugins, where (presumably) the intent is to step through custom Groovy or Java plugin code located in a separate file. I want to set a breakpoint in a Gradle task in a simple build.gradle file, like...
task example {
println "I want to set a breakpoint here"
}
...so that when I run gradle example I can inspect the context in a debugger.
(For those who would point me to IntelliJ...although JetBrains' website advertises that they support debugging Gradle scripts in IDEA UI, AFAICT this is untrue, as this was reported broken in IDEA13 EAP and hasn't been fixed in IDEA14. See Debugging Gradle build files in Intellij / Android Studio )
Is there any debugging tool that allows me to set a breakpoint in a build.gradle file, or is there something about the Gradle DSL that makes it fundamentally impossible to set breakpoints in a task such as my example, above?
There is the easier way:
just add in your command line -Dorg.gradle.debug=true --no-daemon
For example:
gradle nameOfTask -Dorg.gradle.debug=true --no-daemon
Then you should start your IDE and run remote debugging with localhost port 5005, that all.
Gradle is waiting to you, because standard option server=y
org.gradle.debug
When set to true, Gradle will run the build with remote debugging enabled, listening on port 5005. Note that this is the equivalent of adding -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 to the JVM command line and will suspend the virtual machine until a debugger is attached.
Link to docs
IntelliJ 2018.2 added the ability to debug Gradle scripts in a similar fashion to how you might run/debug other projects. You can see the announcement in the release notes here.
Here is a screenshot of some of the documentation from 2018.2:
It does not yet support the kotlin-dsl (see gradle/kotlin-dsl/issues/39).
Personnaly I do this when I need to debug build scripts:
Inside you terminal do
export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
Then run your build
gradle clean install
Finally put some breakpoints and launch the remote debug configuration inside your IDE on the port 5005 and you’re good to go!
export GRADLE_OPTS does not work for me.
Try this:
1 Add remote debug config
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2 add breakpoint in gradle
3 ./gradlew your task --stacktrace -Dorg.gradle.daemon=false -Dorg.gradle.debug=true
4 Attach debug in studio
click icon
Then the breakpoint stops !!
If console is not goiong, click this in debug pannel in studio.
After reading various answers here the following steps will help you debug build.gradle to being able to break and investigate variables inside any custom task. I am using Eclipse remote debugging facilities
Place this simple code where you want to break: try {throw new RuntimeException('Break');} catch (RuntimeException e) {}
As recommended start your task with gradle mytask -Dorg.gradle.debug=true --no-daemon in the command prompt (don't have to do it in Eclipse)
In Eclipse do Run -> Add Java Exception Breakpoint, choose RuntimeException and click "OK"
Again in Eclipse go to Run -> Debug Configurations -> Remote Java Application and create new configuration that listens on localhost:5005. Name it whatever you want. Select a project that contains build.gradle you are debugging. Click Apply and Debug
At this point the execution will start but will pause at the Exception-throwing line. And you can then start looking at your variables in the `Debug -> Variables" view, inspect the stacktrace, step through the code etc.
No magic, alas, you will not see anything highlighted in build.gradle but you can pretty much guess where you are at
Obviously on subsequent runs you don't need step 3 and in 4 you can reuse previously created configuration
If you want to use this in the multiple places simply create a method, use different type of exception and feel free to enhance this idea in any way possible
For example:
void halt() {
try {
throw new RuntimeException('Break');
} catch (RuntimeException e) {
print('Paused')
}
}
task iterateDeclaredDependencies {
doLast {
Object configs = configurations.all
halt();
print(configs)
}
}
I use
set JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8989"
then 2 gradle processes are listening :
Listening for transport dt_socket at address: 8989
Listening for transport dt_socket at address: 8989
I can connect to both of them with 2 remote debug launch configurations in Eclipse although it's the same port.

Launched gradle with a test with the --no-daemon parameter but the daemon is launched anyway

I'm trying to debug some unit tests I've written but the gradle daemon seems to always launch, ignoring any options I've set.
Using Mac OS X 10.9.5, Java 1.7, Gradle 2.2.1 and robolectric-gradle-plugin 0.14.1
Launching gradle with:
GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006"
./gradlew test --no-daemon -Dorg.gradle.debug=true
causes the following line to appear
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: http://gradle.org/docs/2.2.1/userguid....
I've also tried setting the -Xdebug and Xrunjdwp as jvmArgs in build.gradle.
Nothing else happens but if I add -d and rerun, it turns out that the daemon has launched and is waiting on port 5005 for a debugger: http://pastebin.com/TqaXubmr
Finally, if I then launch a debugger attaching to port 5005 the tests run but none of the breakpoints are hit.
The gradle.properties is empty, I haven't set org.gradle.jvmargs.
You might have configure memory settings in your gradle.properties? these can force gradle to launch a new jvm as these settings cannot be applied dynamically.
Keep in mind that unit tests are always executed in a separate jvm. The easiest way to debug tests executed by gradle is to run
>gradle :test --debug-jvm
this will automatically configure your test task to run with debug enabled.

Resources