【问题标题】:Gradle unable to run JUnit tests with useJUnitPlatform()Gradle 无法使用 useJUnitPlatform() 运行 JUnit 测试
【发布时间】:2020-03-09 09:45:48
【问题描述】:

我的项目是使用 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,因为那需要我重写所有这些(工作)测试。

【问题讨论】:

  • 该错误消息表明由于使用了模块,Java 工具/选项存在问题。鉴于您使用的是最新的 Gradle 版本 - 如果您还没有,请先尝试 - 需要有关您的项目设置的更多信息。我建议您创建一个具有最少失败设置的公共存储库/项目。

标签: java gradle javafx junit junit5


【解决方案1】:

好的,我解决了这个问题,我不太确定是什么原因造成的,但它与主模块和测试模块的模块信息有关。我决定暂时将两者都删除,并且它运行正常。

编辑: 经过几天的环顾,我终于弄明白了。添加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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-09
    • 2018-08-19
    • 2014-11-29
    • 2016-03-13
    • 2018-11-17
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多