简体   繁体   English

远程调试使用 Gradle 执行的 JUnit 测试

[英]Remote debug a JUnit test executed using Gradle

I have the following scenario:我有以下情况:

Machine A: Executing a JUnit test using Gradle:机器 A:使用 Gradle 执行 JUnit 测试:

./gradlew :<directory> --tests <ClassName> --info

Machine B: I have an IDE (like IntelliJ).机器 B:我有一个 IDE(如 IntelliJ)。 I want to create a remote Java debug configuration to machine A and debug the test.我想为机器A创建一个远程Java调试配置并调试测试。

I came across " --debug-jvm " option for gradlew for remote debug.我遇到了用于远程调试的 gradlew 的“ --debug-jvm ”选项。 Using this parameter, the Gradle test runner is getting suspended on port 5005 by default.使用此参数,Gradle 测试运行器默认在端口 5005 上暂停。 I can connect to this process only from the same machine (machine A) but not from machine B.我只能从同一台机器(机器 A)连接到此进程,但不能从机器 B 连接。

I followed the steps mentioned at https://www.thomaskeller.biz/blog/2020/11/17/debugging-with-gradle/ But even with this configuration, I am unable to debug from machine B. I always get "Connection refused" error from the IDE in machine B.我按照https://www.thomaskeller.biz/blog/2020/11/17/debugging-with-gradle/中提到的步骤进行操作,但即使使用此配置,我也无法从机器 B 进行调试。我总是得到“连接来自机器 B 中的 IDE 的错误。

With Gradle's --info option, I can see the following parameters added to JVM started for running the tests:使用 Gradle 的 --info 选项,我可以看到添加到 JVM 的以下参数开始运行测试:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005

For remote debugging I believe address should have "*:" before the port number.对于远程调试,我认为地址应该在端口号之前有“*:”。 For example:例如:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005

But the "--debug-jvm" is not adding "*:" to the parameters.但是“--debug-jvm”没有在参数中添加“*:”。

Also tried the following things:还尝试了以下事情:

  1. Adding "debugOptions" to the Gradle's "test" task:将“debugOptions”添加到 Gradle 的“测试”任务中:

    debugOptions { enabled = true port = 8166 server = true suspend = true } debugOptions { 启用 = true 端口 = 8166 服务器 = true 挂起 = true }

The result is the same.结果是一样的。 Not able to connect from machine B and results in "Connection refused" error in IDE.无法从机器 B 连接并导致 IDE 中出现“连接被拒绝”错误。

  1. Adding following properties to gradle.proprties:将以下属性添加到 gradle.proprties:

org.gradle.daemon=false

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

Gradle's daemon thread is suspended with the above properties but not the test executor thread. Gradle 的守护线程被上述属性挂起,但不是测试执行线程。

Gradle version: 5.6.2 Gradle 版本: 5.6.2
Java version: JDK 11 Java 版本: JDK 11

Things are working if everything (running the tests and IDE) is on a single machine.如果一切(运行测试和 IDE)都在一台机器上,那么一切正常。

So the question is, how to really "remote" debug a JUnit test that is executed using Gradle?所以问题是,如何真正“远程”调试使用 Gradle 执行的 JUnit 测试?

Thanks for your help!谢谢你的帮助!

The--debug-jvm option is only designed to allow local debugging. --debug-jvm 选项仅用于允许本地调试。 One workaround is to use jvmArgs on the test task to give it the exact debug arguments you want.一种解决方法是在测试任务上使用 jvmArgs 为其提供所需的精确调试 arguments。 This worked for me.这对我有用。

Exmaple:示例:

jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:30069'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM