我的项目是一个使用 Java JDK 11 的 Gradle 依赖 JavaFX 项目。该项目是使用 Gradle 项目的默认结构制作的。所以在 src 下有“main”和“test”,每个目录由一个“java”和一个“resource”目录组成。
要使用 Jenkins 设置 CI 管道,要求 gradlew test 以与我的 IDE (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$1.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我不建议使用 the same plugin again因为它可能会导致问题,尽管我自己没有尝试过。
那要走什么路呢?那么有两个选择:
-忽略 module-info.java > 这可以在 build.gradle 文件中完成,for an example of that solution have a look at this
-指定 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或this excellent blog by Sormuras which explains a lot
关于java - Gradle 无法使用 useJUnitPlatform() 运行 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839753/