Question
I need to test before/after on dates in a certain test case. I'd like to use Hamcrest matchers if possible.
Are there any matchers for Hamcrest (Java) for working with Dates? If so, what package/class would I find the particular date matcher functions in?
Solution
The OrderingComparison::greaterThan matcher will work on any type which is comparable to itself (it's in the org.hamcrest.number
package, but it's not actually number-specific). Date is such a type.
OTHER TIPS
There is a library of hamcrest date matchers provided by the library at https://github.com/eXparity/hamcrest-date which is also available for maven, ivy, etc at
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>1.1.0</version>
</dependency>
It supports various matchers for dates so allows constructs such as
Date myBirthday = new Date();
MatcherAssert.assertThat(myBirthday, DateMatchers.after(Moments.today()));
or
Date myBirthday = new Date();
MatcherAssert.assertThat(myBirthday, DateMatchers.isToday());
You can have a look at the new Date Matchers that will be added to hamcrest (I don't know when thought):
Date matchers discussion/code changes on github
After a quick look it seems there will be a new package org.hamcrest.date containing:
- IsAfter
- IsBefore
- IsSameDayOfTheMonth
- IsSameDayOfTheWeek
- IsSameDayOfTheYear
- IsSameHour
- IsSameInstant
- IsSameMinute
- IsSameMonth
- IsSameSecond
- IsSameYear
- IsWithin
There are certain hamcrest extensions that can ease some of the testing related to dates. Please check here.
The Matchers#greaterThan
matcher works with Date
s and other Comparable
objects.
Here's the way to check that your date is greater than or equal (≥) to some expected date:
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.core.AnyOf.anyOf;
...
Date expectedMin = new Date()
// Execute the method being tested
Date resultDate = getDate();
// Validate
assertThat(resultDate, anyOf(greaterThan(expectedMin), equalTo(expectedMin)))
There is also the Cirneco extension. It has several Date
specific matchers (e.g. monday()
) and others that apply to dates because of the implementation of Comparable
(see for instance between()
, betweenInclusive()
). The plan is to support also Joda Time in the JDK7 version of the library and the new date-based classes in the JDK8 version (mainly LocalDate
).
You can do assertions like:
final Date date = new Date();
assertThat(date, is(monday())); // JUnit style
given(date).assertIs(monday()); // Cirneco style
You can use the following dependency for a JDK7-compliant project:
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>java7-hamcrest-matchers</artifactId>
<version>0.7.0</version>
</dependency>
or the following if you are using JDK8
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>java8-hamcrest-matchers</artifactId>
<version>0.7.0</version>
</dependency>
https://assertj.github.io/doc/#assertj-core-recursive-comparison
org.assertj:assertj-core:3.12.2
assertThat(actual)
.usingRecursiveComparison()
.ignoringFieldsMatchingRegexes("fieldToIgore")
.isEqualTo(expected);