简体   繁体   English

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

[英]Gradle can't access classes defined in module src/main from src/test with JavaFX plugin

I am trying to allow my test classes to access the main classes (in a standard gradle setup).我试图让我的测试类访问主类(在标准 gradle 设置中)。 It was working fine until I put my main classes in a module (for JavaFX), at which point all tests stopped working.它工作正常,直到我将我的主要类放在一个模块中(用于 JavaFX),此时所有测试都停止工作。 The main code runs fine.主要代码运行良好。

If I understand correctly, according to this gradle documentation , doing nothing should run the tests normally, but I get an error:如果我理解正确,根据这个 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

This happens if I use the intellij configuration, or run ./gradlew test .如果我使用 intellij 配置或运行./gradlew test ,就会发生这种情况。

So I attempted to fix the issue by patching the module-info , but then I get hundreds of errors like this one:所以我试图通过修补 module-info来解决这个问题,但是我得到了数百个像这样的错误:

error: cannot find symbol
import snake.Snake
            ^
  symbol:   class Snake
  location: package snake

Which IntelliJ explains with Package 'snake' is declared in module 'snakegame', which does not export it to module 'snakegame' . IntelliJ 用Package 'snake' is declared in module 'snakegame', which does not export it to module 'snakegame' I'm guessing that it's referring to the original module-info.java which is defined in src/main/java, and the secondary module-info.java in src/test/java.我猜它指的是在 src/main/java 中定义的原始 module-info.java,以及在 src/test/java 中定义的次要 module-info.java。

In the Gradle documentation it had a code snippet for adding the patch argument yourself in the build.gradle, however this just results in this error:Gradle 文档中,它有一个代码片段,用于在 build.gradle 中自己添加补丁参数,但这只会导致此错误:

> java.lang.IllegalArgumentException: error: --patch-module specified more than once for module snakegame

The command which was actually executed looks like this:实际执行的命令如下所示:

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]

So it's patching two different modules, which I don't understand.所以它正在修补两个不同的模块,我不明白。 It doesn't really matter to me how, I just need the classes to be runnable and have access to the main classes.对我来说如何并不重要,我只需要这些类是可运行的并且可以访问主类。

UPDATE:更新:

So I recreated the project to try to create a minimal reproducible example and added in elements one at a time, and the problem didn't show up until I added in the gradle JavaFX plugin, at which point it stopped working.因此,我重新创建了该项目以尝试创建一个最小的可重现示例并一次添加一个元素,直到我添加 gradle JavaFX 插件后问题才出现,此时它停止工作。

So I started with the default structure, simply created a package called example with an Example.class , and created the same package in test, with a test class Example_Test.class with one test that creates an Example object. So I started with the default structure, simply created a package called example with an Example.class , and created the same package in test, with a test class Example_Test.class with one test that creates an Example object. I then added an empty module-info in main.然后我在 main.js 中添加了一个空的模块信息。 Original build.gradle:原装build.gradle:

plugins {
    id 'java'
}
// default stuff

So at this point, everything is working fine.所以在这一点上,一切正常。 Then I change this to:然后我将其更改为:

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

And everything stops working一切都停止工作

This is a strange problem, and many solutions didn't work.这是一个奇怪的问题,许多解决方案都不起作用。

Useful links:有用的链接:

My preferred solution would be to use a test module-info.java or module-info.test, but I was not able to get this to work.我首选的解决方案是使用测试 module-info.java 或 module-info.test,但我无法让它工作。 I ended up simply ignoring modules for tests, which is a passable workaround for the moment, since I'm currently only doing Unit Tests.我最终只是简单地忽略了测试模块,这是目前可以通过的解决方法,因为我目前只进行单元测试。 To ignore modules during testing, add this to the build.gradle:要在测试期间忽略模块,请将其添加到 build.gradle:

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

// other stuff

test {
    useJUnitPlatform()

    moduleOptions {
        runOnClasspath = true
    }
}

Set moduleOptions { runOnClasspath = true } in testtest中设置moduleOptions { runOnClasspath = true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM