Hamcrest - what version to use? 1.3 or 2
Asked Answered
J

3

33

I am quite confused. Currently I am testing my spring application using

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

I was happy as long as I wanted to match RegularExpressions. In hamcrest 1.3 you need to write your own matcher, which I did not like that much. I searched and found that hamcrest 2.0 has something build in, like:

assertThat(DateHelper.getActualDateForXML(), MatchesPattern.matchesPattern("\\d{4}+-\\d{2}-+\\d{2}+T\\d{2}+:\\d{2}+:\\d{2}+"));

I was happy, I added:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-junit</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>

And kicked out the 1.3 hamcrest dependencies from spring-boot-starter-test:

<exclusions>
    <exclusion>
       <groupId>org.hamcrest</groupId>
       <artifactId>hamcrest-library</artifactId>
    </exclusion>
    <exclusion>
       <groupId>org.hamcrest</groupId>
       <artifactId>hamcrest-core</artifactId>
    </exclusion>
</exclusions>

Now everything is still working as I expect it to work, but I do not feel really comfortable. Because I can just find people writing about 1.3 and can not really find the use of hamcrest-junit 2.0.

Can somebody explain me the connection between them? Because it seems that hamcrest-junit 2.0 has everything build in from hamcrest 1.3...

Thanks Ben

Jolda answered 18/11, 2015 at 9:56 Comment(2)
You can't find documentation on the difference between 2.0 and 1.3 because the project [has not been properly maintained for quite a few months][1]. If an active committer takes over, then we will all know. Thanks @CoronA.Demerol
Related: Snippet on how to setup Junit with hamcrest propertlySherrisherrie
W
40

EDIT: After several years the answer is to use the latest Hamcrest 2 version (2.2 from 17th October 2019). For additional details also refer to @dschulten's answer.

Following is my original answer which I leave as context to understand the problem and confusion around Hamcrest versions 1.3 and 2.0.0.0 back in the day.


Based on Hamcrest Github

and JUnit Lambda (Junit 5)

My take on it

  • hamcrest-junit 2.0 (should be) is a fresh start with also the goal to decouple hamcrest from junit
  • in the meantime Junit 5 project kicked off which reduces/removes third party dependencies

=> In this situation, I expect it is prudent for the hamcrest guys to wait for / coordinate with the JUnit 5 project before moving forward substantially.

As @heenenee mentioned, the hamcrest guys became busy with other stuff and so not much is happening with the project at this point.

To answer

Can somebody explain me the connection between them? Because it seems that hamcrest-junit 2.0 has everything build in from hamcrest 1.3

  • hamcrest-junit 2.0 started but the guys behind it got busy soon after (and still are) with other projects, so the development stopped
  • there might also be some uncertainties regarding JUnit 5 which may be an incentive to defer further hamcrest-junit 2.0 development until the JUnit 5 release (speculation)

... but I do not feel really comfortable. Because I can just find people writing about 1.3 and can not really find the use of hamcrest-junit 2.0.

At the moment, other than for your case there is not much incentive to move to hamcrest-junit 2.0. Once Junit 5 releases I expect that there will be more incentive to move forward again.

Wallow answered 9/1, 2016 at 19:14 Comment(3)
JUnit 5 is here and they decoupled testing framework from assertion framework. Since that time up to 2018 there haven't been releases of Hamcrest. On other hands AspectJ is sound alternative.Concertmaster
@Concertmaster AspectJ comes with various strings attached, so it's not a viable alternative for every project.Gerstein
Changelog of hamcrest is now at github.com/hamcrest/JavaHamcrest/blob/master/CHANGES.mdImply
I
12

The Hamcrest project has just released 2.1, and they reworked the package structure after thorough consideration. Make sure you follow the upgrade procedure. The artifact java-hamcrest 2.0.0.0 is considered a failed attempt now.

Imply answered 6/1, 2019 at 10:23 Comment(1)
Almost didnt see this answer due to the low vote count. thx for updateUniversally
G
3

Great answer by Ivo, which really should be getting more votes, but here's some added information. If you check Maven for information on the dependencies, Java Hamcrest 2.0.0.0 and Hamcrest JUnit 2.0.0.0, there are some usage links. At the time I write this, it's 74 and 68 respectively, and no major projects from a brief browsing.

Goodrich answered 21/2, 2016 at 4:29 Comment(1)
this has changed quite a bit, it now stands at 500 usages, among them some major projects...Incoming

© 2022 - 2024 — McMap. All rights reserved.