gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:48:40 24 4
gpt4 key购买 nike

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

我认为问题在于 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

最佳答案

修复方法是使用 truth.java8.extension 修补 truth。这可以在 gradle 中使用 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 的路径。

关于java - 如何修复测试依赖项中的拆分包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56873136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com