39

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?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
smp7d
  • 4,947
  • 2
  • 26
  • 48

7 Answers7

41

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.

domids
  • 515
  • 5
  • 21
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • Thanks. It seems they've got rid of the class in favour of a static factory method, which makes a really stable link impossible, but i've fixed it as far as i can. – Tom Anderson Sep 28 '13 at 20:57
  • That's true. There are also extensions that provide some more easy-to-read methods. E.g., [Cirneco](https://github.com/ozimov/cirneco) provides the matcher `J7Matchers::after` that is an alias for `OrderingComparison::greaterThan`. From my point of view, _sematic_ is always important in unit test, that's why I usually prefer the fulent approach provided by Google Truth, but I sometimes have to handle with Hamcrest in legacy projects. – JeanValjean Jan 09 '16 at 23:31
19

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());
stewbis
  • 370
  • 3
  • 10
7

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
4

There are certain hamcrest extensions that can ease some of the testing related to dates. Please check here.

johan
  • 518
  • 6
  • 18
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

The Matchers#greaterThan matcher works with Dates 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)))
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
-1

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>
JeanValjean
  • 17,172
  • 23
  • 113
  • 157
-2

https://assertj.github.io/doc/#assertj-core-recursive-comparison

org.assertj:assertj-core:3.12.2

assertThat(actual)
    .usingRecursiveComparison()
    .ignoringFieldsMatchingRegexes("fieldToIgore")
    .isEqualTo(expected);