簡體   English   中英

Gradle 無法使用 JavaFX 插件從 src/test 訪問模塊 src/main 中定義的類

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

我試圖讓我的測試類訪問主類(在標准 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 中自己添加補丁參數,但這只會導致此錯誤:

> 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 插件后問題才出現,此時它停止工作。

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. 然后我在 main.js 中添加了一個空的模塊信息。 原裝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
    }
}

test中設置moduleOptions { runOnClasspath = true }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM