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
hamcrest-core. jar : This was the core API to be used by third-party framework providers. This includes a foundation set of matcher implementations for common operations. This library was used as a dependency for many third-party libraries, including JUnit 4.
Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit. You can also define custom matcher implementations.
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
Hamcrest is the well-known framework used for unit testing in the Java ecosystem. It's bundled in JUnit and simply put, it uses existing predicates – called matcher classes – for making assertions.
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
=> 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
... 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With