温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - How to fix a split-package in test dependencies?

java - 如何在测试依赖项中修复拆分包?

发布于 2020-03-27 11:43:53

我创建了一个示例项目来解释我遇到的问题。我有使用两个具有拆分包的依赖罐的测试。在这种情况下,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 /测试/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.

我认为,问题是,truthtruth.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

查看更多

查看更多

提问者
John Mercier
被浏览
31
John Mercier 2019-07-04 01:46

解决方法是修补truthtruth.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的路径。

发布
问题

分享
好友

手机
浏览

扫码手机浏览