I have been into this for a while. The related questions in StackOverflow are about the absence of the hamcrest-core
JAR in the classpath of the project, and the solutions are all related to its addition. What I am trying to do is de opposite: removing this dependency from the classpath.
Consider a Maven project with a single test case:
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);
}
}
Here the test shouldAnswerWithTrue
invokes the method assertTrue
in the class Assert
from the dependency junit
version 4.11 (declared in the POM). When I construct the corresponding call graph, the dependency hamcrest-core
seems not to be used in this test case. hamcrest-core
is a transitive dependency induced by the direct dependency junit
. Therefore, I proceed to exclude it from the POM of my project as follows:
<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>
However, when I execute mvn package
, it triggers the following error:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
I do not understand why Java is complaining about the interface SelfDescribing
in a dependency that is not used at all in my test and in any of its methods calls. I have checked that no class from hamcrest-core
is loaded from the Assert
classes in JUnit.
So, Why I cannot exclude hamcrest-core
? Why is this interface needed? Where is it called?
Because JUnit 4.11 actually depends on it at compile time: it uses it in its exception hierarchy . When the AssumptionViolatedException
class is loaded, it will trigger a load of SelfDescribing
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.