Gradle无法使用useJUnitPlatform()运行JUnit测试 [英] Gradle unable to run JUnit tests with useJUnitPlatform()

查看:635
本文介绍了Gradle无法使用useJUnitPlatform()运行JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是使用Java JDK 11的依赖Gradle的JavaFX项目.该项目是使用Gradle项目的默认结构创建的.因此,在src下,它具有"main"和"test",每个目录都由一个"java"目录和一个"resource"目录组成.

My project is a Gradle dependent JavaFX project using Java JDK 11. The project was made using the default structure of a Gradle project. So under src it has "main" and "test", each consisting of a "java" and a "resource" directory.

要使用Jenkins设置CI管道,要求 gradlew测试以到目前为止我的IDE(IntelliJ)相同的方式运行所有测试.有趣的是,经过数小时的尝试,我仍无法使它正常工作.

For setting up a CI pipeline with Jenkins it is a requirement that gradlew test runs all tests in the same way my IDE (IntelliJ) so far has. The funny thing is, I have not been able to get this to work after hours of trying.

在IntelliJ运行配置中,它在.test软件包中测试全部在软件包中",一切正常(点击截屏)下面是build.gradle文件的所有相关部分,请注意,为简单起见,我省略了所有不相关的部分.

In the IntelliJ run configuration it tests "all in package", within the .test package, and this works just fine (click for screenshot) Below are all the relevant parts of the build.gradle file, please note that I have left out all unrelated parts for simplicity.

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命令时,都会出现以下两个错误:

Whenever I run the gradlew test command I get the following two errors:

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().乍一看,这似乎工作得很好,但是当我使用它时,根本没有发现任何测试.我的假设是测试的默认模块设置不正确.但是,我发现没有办法在网上任何地方设置或不提及此内容.请让我知道您的想法:)

Another approach I have tried is to use the useJUnit() command instead of useJUnitPlatform(). At first sight this seems to work perfectly fine however when I use this no tests are found at all. My assumption is that the default module for tests is not set correctly. However I have found no way to set this or no mention of this anywhere online. Please let me know what your thoughts are :)

PS.我宁愿不降级到JUnit 4.x,因为那将需要我重写所有这些(有效的)测试.

PS. I would much rather not downgrade to JUnit 4.x since that would require me to rewrite all these (working) tests.

推荐答案

好的,我解决了这个问题,我不太确定到底是什么原因造成的,但是它与main和test的module-info有关模块.我决定暂时将两者都删除,并且它会按预期运行.

Alright I solved the issue, I'm not quite sure what exactly caused it but it had something to do with the module-info for both the main and test modules. I decided to remove both for the time being and it runs like it should.

经过数天的环顾,我终于明白了.添加 module-info.java 会封装一个模块及其依赖项.因为此项目再次使用相同的插件,尽管我自己还没有尝试过该插件,但可能会导致问题.

After days of looking around I got it figured out at last. Adding a module-info.java encapsulates a module and it's dependencies. Because this project applies the gradle-modules-plugin I wouldn't recommend using the same plugin again as it could cause issues though I have not experimented with this myself.

那要走什么路呢?嗯,有两种选择:

What is the way to go then? Well there are two options:

-忽略module-info.java>可以在build.gradle文件

-Ignore the module-info.java > this can be done in the build.gradle file, for an example of that solution have a look at this

-指定module-info.test>这是一种应用某些依赖项的方法,我的样子如下所示:

-Specify a module-info.test > this is a way to apply certain dependencies, mine looks like the following:

--add-modules
        org.junit.jupiter.api

--add-reads
        FP.EDM.main=org.junit.jupiter.api

请记住:这种文件格式应该放在源代码的根目录中,我所有的测试文件都位于根目录中的另一个目录中,因为我没有使其在根目录中起作用(尽管可以就是我).

Please do keep in mind: this file format should go in the root of your source, all my test files were in a different directory in the root, as I didn't get it to work in the root (though that could just be me).

要进一步阅读,请参阅此Git问题首先帮助我解决了该问题这个出色的博客由Sormuras撰写,解释了很多内容

For further reading there is this Git issue which helped me solve it in the first place or this excellent blog by Sormuras which explains a lot

这篇关于Gradle无法使用useJUnitPlatform()运行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆