我试图允许我的测试类访问主类(在标准gradle设置中)。直到我将主类放在模块中(对于JavaFX)为止,它一直运行良好,这时所有测试都停止了工作。主代码运行良好。
如果我正确理解,根据此gradle文档,什么也不做应该可以正常运行测试,但是出现错误:
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
...
Caused by: java.lang.IllegalAccessError: class org.junit.platform.launcher.core.LauncherFactory (in unnamed module @0x50650eec) cannot access class org.junit.platform.commons.util.Preconditions (in module org.junit.platform.commons) because module org.junit.platform.commons does not export org.junit.platform.commons.util to unnamed module @0x50650eec
如果我使用intellij配置或运行,则会发生这种情况./gradlew test
。
因此,我尝试通过修补module-info来解决此问题,但随后出现了数百个类似这样的错误:
error: cannot find symbol
import snake.Snake
^
symbol: class Snake
location: package snake
哪个是IntelliJ解释的Package 'snake' is declared in module 'snakegame', which does not export it to module 'snakegame'
。我猜它是指在src / main / java中定义的原始module-info.java和在src / test / java中的辅助module-info.java。
在Gradle文档中,有一个代码片段可用于在build.gradle中自己添加patch参数,但这只会导致以下错误:
> java.lang.IllegalArgumentException: error: --patch-module specified more than once for module snakegame
实际执行的命令如下所示:
compiler args for task compileTestJava: [--patch-module, snakegame=/project/path/snake/build/classes/java/main, --module-path, ... , --add-modules, org.junit.jupiter.api, --add-reads, snakegame=org.junit.jupiter.api, --patch-module, snakegame=/project/path/snake/src/test/java]
因此,它修补了两个我不理解的模块。对于我来说,这并不重要,我只需要这些类是可运行的并可以访问主类即可。
因此,我重新创建了该项目,以尝试创建一个最小的可复制示例,并一次添加一个元素,直到我添加了gradle JavaFX插件,该问题才出现,此时它停止了工作。
所以,我开始使用默认的结构,简单地创建了一个称为包example
带Example.class
,并且在测试中创建的相同的封装,具有一个测试类Example_Test.class
与创建一个实施例对象的一个试验。然后,我在main中添加了一个空的module-info。原始build.gradle:
plugins {
id 'java'
}
// default stuff
因此,此时一切正常。然后我将其更改为:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
一切都停止了
这是一个奇怪的问题,许多解决方案都行不通。
有用的链接:
我的首选解决方案是使用测试module-info.java或module-info.test,但是我无法使其正常工作。我最终只是忽略了测试模块,这是目前可以通过的解决方法,因为我目前仅进行单元测试。要在测试期间忽略模块,请将其添加到build.gradle中:
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
// other plugins
}
// other stuff
test {
useJUnitPlatform()
moduleOptions {
runOnClasspath = true
}
}
设置moduleOptions { runOnClasspath = true }
在test
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句