Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hamcrest core, why need this?

I was using JUnit for my TDD in Java and noticed there are two components to download from JUnit.org. First of all, I thought I need JUnit component and downloaded, installed. When I compiled and tried to run my test, it was complaining about Hamcrest classes are not found. So I had to download this one again from their homepage.

So, out of curiosity, why the heck would we need two downloads for one purpose usage from the beginning? Does anyone know why hamcrest core is separate from JUnit, even though it is used by JUnit?

Thanks, Javabug

like image 640
Young Avatar asked Dec 24 '22 05:12

Young


2 Answers

JUnit uses Hamcrest. In the past JUnit was embedding the Hamcrest classes which lead to problems, as the projects were evolving in different cycles. In recent JUnit versions (if I'm not wrong, since 4.11) this has been changed and Hamcrest is not embedded. So if you add JUnit as dependency to your project (Maven, Gradle, etc) you will implicit get a dependency to Hamcrest.

I believe this issue on Hamcrest is somehow related to that splitting. https://github.com/hamcrest/JavaHamcrest/issues/92

like image 85
SubOptimal Avatar answered Jan 09 '23 00:01

SubOptimal


The class org.junit.Assert has a dependency on Hamcrest core library. As it is part of the static method signature assertThat(), it has to be on the classpath.

If you won't like to use it, a test runtime dependency on hamcrest-core should work, if you want to use it, I recommend a test compile dependency on hamcrest-library (using maven instead of gradle it's all scope test).

There is no transient dependency to be more flexible to upgrade Hamcrest. Btw also Mockito has a Hamcrest dependency. Read more about Understanding Dependencies.

like image 33
Arne Burmeister Avatar answered Jan 09 '23 01:01

Arne Burmeister