Hamcrest日期匹配器

smp*_*p7d 30 java date hamcrest

我需要在特定测试用例的日期之前/之后进行测试.如果可能的话,我想使用Hamcrest匹配器.

Hamcrest(Java)是否有与Dates合作的匹配器?如果是这样,我会找到特定日期匹配器功能的包/类?

Tom*_*son 34

OrderingComparison :: GREATERTHAN匹配会在任何类型的这相当于本身(它在工作org.hamcrest.number包,但它实际上不是特定数字).日期就是这样一种类型.


ste*_*bis 15

图书馆在https://github.com/eXparity/hamcrest-date提供了一个hamcrest日期匹配器库,也可用于maven,ivy等.

<dependency>
    <groupId>org.exparity</groupId>
    <artifactId>hamcrest-date</artifactId>
    <version>1.1.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

它支持日期的各种匹配器,因此允许构造如

Date myBirthday = new Date();
MatcherAssert.assertThat(myBirthday, DateMatchers.after(Moments.today()));
Run Code Online (Sandbox Code Playgroud)

要么

Date myBirthday = new Date();
MatcherAssert.assertThat(myBirthday, DateMatchers.isToday());
Run Code Online (Sandbox Code Playgroud)


Gui*_*oso 5

您可以查看将添加到hamcrest的新日期匹配器(我不知道何时想到):

github上的日期匹配器讨论/代码更改

快速浏览之后,似乎会有一个新的包org.hamcrest.date包含:

  • 之后
  • 是之前
  • 每月同一天
  • IsSameDayOfTheWeek
  • 一年中的同一天
  • 等时
  • 即时
  • IsSameMinute
  • IsSameMonth
  • 是第二个
  • 同年
  • IsWithin