Java 如果我';我在Maven测试中根本不用hamcrest?
我对此已经有一段时间了。StackOverflow中的相关问题是关于项目的类路径中缺少Java 如果我';我在Maven测试中根本不用hamcrest?,java,maven,junit,dependencies,hamcrest,Java,Maven,Junit,Dependencies,Hamcrest,我对此已经有一段时间了。StackOverflow中的相关问题是关于项目的类路径中缺少hamcrest coreJAR,解决方案都与它的添加有关。我试图做的是反求:从类路径中删除这个依赖项 考虑一个带有单个测试用例的Maven项目: import static org.junit.Assert.assertTrue; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { /
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);
}
}
这里,testshouldAnswerWithTrue
从依赖项junit
版本4.11(在POM中声明)调用类Assert
中的方法assertTrue
。当我构造相应的调用图时,依赖关系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>
我不明白为什么Java抱怨依赖项中的接口selfdescription
,而在我的测试和它的任何方法调用中根本没有使用该接口。我已经检查了JUnit中的Assert
类是否没有加载来自hamcrest-core
的类
那么,为什么我不能排除hamcrest core?为什么需要这个接口?它的名称是什么?因为JUnit4.11在编译时实际上依赖于它:it。加载
AssumptionViolatedException
类时,它将触发加载自描述
问题非常简单。如果您查看junit的pom文件,您会发现有一个对hamcrest定义的依赖项,这意味着它是必需的。最后,排除项目管理员决定使用的依赖项是没有意义的。因此,得到NoClassDefFoundError并不奇怪。我刚刚对给定的示例执行了mvn编译器:testCompile-Dverbose=true
。但是,没有根据输出加载hamcrest中的类。如何查看自描述的负载?
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing