如果我在maven测试中根本没有使用hamcrest,为什么我会得到noclassdeffound org/hamcrest/selfdescription?

pgccezyw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(311)

我对这件事已经有一段时间了。stackoverflow中的相关问题是关于 hamcrest-core jar在项目的类路径中,解决方案都与它的添加有关。我要做的是反求:从类路径中删除这个依赖关系。
考虑一个带有单个测试用例的maven项目:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue() {
        assertTrue(true);
    }
}

这是测试 shouldAnswerWithTrue 调用方法 assertTrue 在班上 Assert 从依赖关系 junit 版本4.11(在pom中声明)。当我构造相应的调用图时,依赖关系 hamcrest-core 似乎没有在这个测试用例中使用。 hamcrest-core 是由直接依赖关系引起的传递依赖关系 junit . 因此,我将其从我的项目pom中排除如下:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然而,当我执行 mvn package ,它会触发以下错误:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

我不明白为什么java会抱怨这个接口 SelfDescribing 在我的测试及其任何方法调用中根本没有使用的依赖项中。我已经查过了 hamcrest-core 是从 Assert junit课程。
那么,为什么我不能排除 hamcrest-core ? 为什么需要这个接口?它叫什么名字?

n3ipq98p

n3ipq98p1#

因为JUnit4.11实际上在编译时依赖于它:它在其异常层次结构中使用它。当 AssumptionViolatedException 类,它将触发 SelfDescribing .

相关问题