Gradle 无法 运行 使用 useJUnitPlatform() 进行 JUnit 测试
Gradle unable to run JUnit tests with useJUnitPlatform()
我的项目是一个 Gradle 依赖 JavaFX 项目,使用 Java JDK 11. 该项目是使用 Gradle 的默认结构制作的项目。所以在 src 下它有 "main" 和 "test",每个由一个 "java" 和一个 "resource" 目录组成。
要使用 Jenkins 设置 CI 管道,要求 gradlew test 运行 以我的 [=41] 相同的方式进行所有测试=] (IntelliJ) 到目前为止。有趣的是,经过几个小时的尝试,我还是无法让它工作。
In the IntelliJ run configuration it tests "all in package", within the .test package, and this works just fine (click for screenshot) 以下是 build.gradle 文件的所有相关部分,请注意,为了简单起见,我省略了所有不相关的部分。
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
}
test {
useJUnitPlatform()
}
application {
mainClassName = 'FP.EDM.main/edm.Main'
}
每当我 运行 gradlew test 命令时,我都会收到以下两个错误:
java.lang.RuntimeException: Unable to parse --add-opens <module>/<package>: FP.EDM.main/
Could not write standard input to Gradle Test Executor 1.
java.io.IOException: The pipe is being closed
at java.base/java.io.FileOutputStream.writeBytes(Native Method)
at java.base/java.io.FileOutputStream.write(FileOutputStream.java:354)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.forwardContent(ExecOutputHandleRunner.java:67)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.run(ExecOutputHandleRunner.java:52)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:46)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.base/java.lang.Thread.run(Thread.java:834)
我尝试过的另一种方法是使用 useJUnit() 命令而不是 useJUnitPlatform()。乍一看,这似乎工作得很好,但是当我使用它时,根本没有发现任何测试。我的假设是测试的默认模块设置不正确。但是我发现没有办法在网上任何地方设置这个或没有提到这个。请让我知道您的想法:)
PS。我宁愿不降级到 JUnit 4.x,因为这需要我重写所有这些(工作)测试。
好的,我解决了这个问题,我不太清楚到底是什么原因造成的,但它与主模块和测试模块的模块信息有关。我决定暂时删除两者,并且它可以正常运行。
编辑:
经过几天的环顾四周,我终于明白了。添加 module-info.java
封装了一个模块及其依赖项。因为这个项目 applies the gradle-modules-plugin I wouldn't recommend using the same plugin again 因为它可能会导致问题,虽然我自己还没有试验过。
那该怎么走呢?那么有两个选择:
-忽略模块-info.java > 这可以在 build.gradle 文件中完成,for an example of that solution have a look at this
-Specify a module-info.test > 这是应用某些依赖项的方法,我的如下所示:
--add-modules
org.junit.jupiter.api
--add-reads
FP.EDM.main=org.junit.jupiter.api
请记住:这种文件格式应该放在你的源代码的根目录中,我所有的测试文件都在根目录中的不同目录中,因为我没有让它在根目录中工作(虽然那可能只是我)。
进一步阅读有this Git issue which helped me solve it in the first place or this excellent blog by Sormuras which explains a lot
我的项目是一个 Gradle 依赖 JavaFX 项目,使用 Java JDK 11. 该项目是使用 Gradle 的默认结构制作的项目。所以在 src 下它有 "main" 和 "test",每个由一个 "java" 和一个 "resource" 目录组成。
要使用 Jenkins 设置 CI 管道,要求 gradlew test 运行 以我的 [=41] 相同的方式进行所有测试=] (IntelliJ) 到目前为止。有趣的是,经过几个小时的尝试,我还是无法让它工作。
In the IntelliJ run configuration it tests "all in package", within the .test package, and this works just fine (click for screenshot) 以下是 build.gradle 文件的所有相关部分,请注意,为了简单起见,我省略了所有不相关的部分。
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
}
test {
useJUnitPlatform()
}
application {
mainClassName = 'FP.EDM.main/edm.Main'
}
每当我 运行 gradlew test 命令时,我都会收到以下两个错误:
java.lang.RuntimeException: Unable to parse --add-opens <module>/<package>: FP.EDM.main/
Could not write standard input to Gradle Test Executor 1.
java.io.IOException: The pipe is being closed
at java.base/java.io.FileOutputStream.writeBytes(Native Method)
at java.base/java.io.FileOutputStream.write(FileOutputStream.java:354)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.forwardContent(ExecOutputHandleRunner.java:67)
at org.gradle.process.internal.streams.ExecOutputHandleRunner.run(ExecOutputHandleRunner.java:52)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl.run(ManagedExecutorImpl.java:46)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.base/java.lang.Thread.run(Thread.java:834)
我尝试过的另一种方法是使用 useJUnit() 命令而不是 useJUnitPlatform()。乍一看,这似乎工作得很好,但是当我使用它时,根本没有发现任何测试。我的假设是测试的默认模块设置不正确。但是我发现没有办法在网上任何地方设置这个或没有提到这个。请让我知道您的想法:)
PS。我宁愿不降级到 JUnit 4.x,因为这需要我重写所有这些(工作)测试。
好的,我解决了这个问题,我不太清楚到底是什么原因造成的,但它与主模块和测试模块的模块信息有关。我决定暂时删除两者,并且它可以正常运行。
编辑:
经过几天的环顾四周,我终于明白了。添加 module-info.java
封装了一个模块及其依赖项。因为这个项目 applies the gradle-modules-plugin I wouldn't recommend using the same plugin again 因为它可能会导致问题,虽然我自己还没有试验过。
那该怎么走呢?那么有两个选择:
-忽略模块-info.java > 这可以在 build.gradle 文件中完成,for an example of that solution have a look at this
-Specify a module-info.test > 这是应用某些依赖项的方法,我的如下所示:
--add-modules
org.junit.jupiter.api
--add-reads
FP.EDM.main=org.junit.jupiter.api
请记住:这种文件格式应该放在你的源代码的根目录中,我所有的测试文件都在根目录中的不同目录中,因为我没有让它在根目录中工作(虽然那可能只是我)。
进一步阅读有this Git issue which helped me solve it in the first place or this excellent blog by Sormuras which explains a lot