Hamcrest Date Matchers
Asked Answered
A

7

40

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?

Apiary answered 23/12, 2011 at 20:5 Comment(0)
C
42

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.

Convolvulus answered 23/12, 2011 at 21:23 Comment(2)
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.Convolvulus
That's true. There are also extensions that provide some more easy-to-read methods. E.g., 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.Huddersfield
Y
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());
Yser answered 8/8, 2013 at 17:6 Comment(0)
Y
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
Yesima answered 1/2, 2013 at 10:42 Comment(0)
K
4

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

Keramic answered 23/12, 2011 at 20:28 Comment(0)
M
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)))
Minardi answered 29/6, 2017 at 12:40 Comment(0)
H
-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>
Huddersfield answered 31/12, 2015 at 8:38 Comment(0)
S
-2

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

org.assertj:assertj-core:3.12.2

assertThat(actual)
    .usingRecursiveComparison()
    .ignoringFieldsMatchingRegexes("fieldToIgore")
    .isEqualTo(expected);
Sink answered 7/6, 2019 at 18:22 Comment(1)
How is this related to the topic?Dendrology

© 2022 - 2024 — McMap. All rights reserved.