Young
Young

Reputation: 73

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

Upvotes: 4

Views: 5568

Answers (3)

Arne Burmeister
Arne Burmeister

Reputation: 20604

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.

Upvotes: 3

GhostCat
GhostCat

Reputation: 140465

Actually, you want to use as much of the "hamcrest" stuff as possible.

Have a look at assertThat, which makes heavy use of hamcrest matcher classes.

Seriously: I am only using assertThat in my testy by now; and I never ever regretted doing so and abandoning all those other asserts altogether.

Upvotes: 1

SubOptimal
SubOptimal

Reputation: 22963

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

Upvotes: 6

Related Questions