我创建了一个示例项目来解释我遇到的问题。我有使用两个具有拆分包的依赖罐的测试。在这种情况下,google-truth具有split-package。
build.gradle
plugins {
id 'java-library'
id 'org.javamodularity.moduleplugin' version '1.5.0'
}
group 'example'
version '1.0-SNAPSHOT'
sourceCompatibility = 11
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'com.google.truth:truth:0.42'
testImplementation 'com.google.truth.extensions:truth-java8-extension:0.42'
}
src / main / java / module-info.java
module truth.tests.main {
exports example.truthtest;
}
src / test / java / module-info.test
--add-reads
truth.tests.main=truth
src / test / java / example / truthtest / ATest.java
package example.truthtest;
import org.junit.Test;
import static com.google.common.truth.Truth.assertThat;
public class ATest {
@Test
public void test() {
assertThat(15).isEqualTo(15);
}
}
运行构建时,出现以下错误。
> Task :compileTestJava FAILED
error: the unnamed module reads package com.google.common.truth from both truth and truth.java8.extension
error: module truth reads package com.google.common.truth from both truth and truth.java8.extension
error: module hamcrest.core reads package com.google.common.truth from both truth and truth.java8.extension
error: module com.google.common reads package com.google.common.truth from both truth and truth.java8.extension
error: module checker.compat.qual reads package com.google.common.truth from both truth and truth.java8.extension
error: module checker.qual reads package com.google.common.truth from both truth and truth.java8.extension
error: module diffutils reads package com.google.common.truth from both truth and truth.java8.extension
error: module auto.value.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module error.prone.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module jsr305 reads package com.google.common.truth from both truth and truth.java8.extension
error: module j2objc.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module animal.sniffer.annotations reads package com.google.common.truth from both truth and truth.java8.extension
error: module truth.java8.extension reads package com.google.common.truth from both truth and truth.java8.extension
error: module junit reads package com.google.common.truth from both truth.java8.extension and truth
error: module truth.tests.main reads package com.google.common.truth from both truth and truth.java8.extension
15 errors
FAILURE: Build failed with an exception.
我认为问题在于truth
和truth.java8.extension
都在同一包com.google.truth
中包含类。如果删除truth-java8-extensions
,则测试会编译,但这是我实际项目中必需的依赖项。如何使用Java的模块系统使这两个模块在测试中协同工作?
当我调试gradle时,这些是失败的编译器选项(路径被截断了)。
Compiler arguments:
-source 11
-target 11
-d build\classes\java\test
-g
-sourcepath
-proc:none
-s build\generated\sources\annotationProcessor\java\test -XDuseUnsharedTable=true
-classpath
--module-path build\classes\java\main;build\resources\main;truth-java8-extension-0.42.jar;truth-0.42.jar;junit-4.12.jar;hamcrest-core-1.3.jar;guava-25.1-android.jar;checker-compat-qual-2.5.3.jar;checker-qual-2.5.3.jar;diffutils-1.3.0.jar;auto-value-annotations-1.6.2.jar;error_prone_annotations-2.3.1.jar;jsr305-3.0.2.jar;j2objc-annotations-1.1.jar;animal-sniffer-annotations-1.14.jar
--patch-module truth.tests.main=src\test\java
--add-modules junit
--add-reads truth.tests.main=junit src\test\java\example\truthtest\ATest.java
答案 0 :(得分:1)
解决方法是用truth
修补truth.java8.extension
。可以使用patchModules来实现。
patchModules.config = [
"truth=truth-java8-extension-0.42.jar"
]
此配置产生以下结果:
Compiler arguments:
-source 11
-target 11
-d build\classes\java\test
-g
-sourcepath
-proc:none
-s build\generated\sources\annotationProcessor\java\test
-XDuseUnsharedTable=true
-classpath
--module-path build\classes\java\main;build\resources\main;truth-0.42.jar;junit-4.12.jar;hamcrest-core-1.3.jar;guava-25.1-android.jar;checker-compat-qual-2.5.3.jar;checker-qual-2.5.3.jar;diffutils-1.3.0.jar;auto-value-annotations-1.6.2.jar;error_prone_annotations-2.3.1.jar;jsr305-3.0.2.jar;j2objc-annotations-1.1.jar;animal-sniffer-annotations-1.14.jar
--patch-module truth.tests.main=src\test\java
--add-modules junit
--add-reads truth.tests.main=junit
--patch-module truth=${gradle-path}\truth-java8-extension-0.42.jar
--add-reads truth.tests.main=truth src\test\java\example\truthtest\ATest.java
有了真实的扩展程序补丁,测试现在可以工作了。不错的是,此配置会自动填充jar的路径。