【问题标题】:Gradle can't access classes defined in module src/main from src/test with JavaFX pluginGradle 无法使用 JavaFX 插件从 src/test 访问模块 src/main 中定义的类
【发布时间】:2021-05-21 21:12:35
【问题描述】:

我试图让我的测试类访问主类(在标准 gradle 设置中)。它工作正常,直到我将我的主要类放在一个模块中(用于 JavaFX),此时所有测试都停止工作。主代码运行良好。

如果我理解正确,根据this gradle documentation,什么都不做应该可以正常运行测试,但是我得到一个错误:

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,就会发生这种情况。

所以我尝试通过patching the 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 documentation 中有一个代码 sn-p 用于在 build.gradle 中自己添加补丁参数,但这只会导致此错误:

> 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.js 中添加了一个空的模块信息。原始 build.gradle:

plugins {
    id 'java'
}
// default stuff

所以在这一点上,一切正常。然后我将其更改为:

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

一切都停止了

【问题讨论】:

标签: java gradle javafx junit java-module


【解决方案1】:

我首选的解决方案是使用测试 module-info.java 或 module-info.test,但我无法让它工作。我最终选择了simply ignoring modules 进行测试,这是一个糟糕的解决方法,但目前有效。要在测试期间忽略模块,请将其添加到 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 }

有用的链接:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多