Gradle 无法使用 JavaFX 插件从 src/test 访问模块 src/main 中定义的类



我试图让我的测试类访问主类(在标准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

如果我使用intelllij配置,或者运行./gradlew test,就会发生这种情况。

所以我试图通过修补模块信息来解决这个问题,但后来我得到了数百个这样的错误:

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中的次要模块-info.java。

在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.class创建了一个名为example的包,并在test中创建了相同的包,用一个测试类Example_Test.class创建了一个创建示例对象的测试。然后我在main中添加了一个空模块-info。原始build.gradle:

plugins {
id 'java'
}
// default stuff

在这一点上,一切都很好。然后我把它改成:

plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
}

一切都停止工作

我的首选解决方案是使用测试模块-info.java或模块-info。测试,但我没能让它工作。我最终只是忽略模块进行测试,这是一个糟糕的解决方法,但目前可行。要在测试期间忽略模块,请将此添加到build.gradle:

plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.9'
// other plugins
}
// other stuff
test {
useJUnitPlatform()
moduleOptions {
runOnClasspath = true
}
}

test中设置moduleOptions { runOnClasspath = true }

的有用链接:

  • https://stackoverflow.com/a/58854685/12393574
  • https://github.com/junit-team/junit5/issues/2111 issuecomment - 557925312
  • https://sormuras.github.io/blog/2018-09-11-testing-in-the-modular-world.html
  • https://stackoverflow.com/a/58990250/12393574
  • JavaFX插件添加gradle-modules-plugin

相关内容

最新更新