我一直在寻找,但我没有找到任何有用的东西。是否有任何与hamcrest一起使用的第三方库具有广泛的日期匹配?
具体来说,我正在寻找以下方面的匹配:
assertThat(myDate, is(withinMinutes(sourceDate, 10)));
assertThat(myDate, is(afterDate(sourceDate)));
assertThat(myDate, is(betweenDates(startDate, endDate)));
在我推出自己的产品之前,我想知道是否有任何东西。
答案 0 :(得分:3)
我写了一组日期匹配器,看起来就像你追求的那样。来源是https://github.com/eXparity/hamcrest-date。如何使用内部匹配器的示例
assertThat(dateUnderTest, DateMatchers.within(2, TimeUnit.SECONDS, new Date()));
您可以使用maven将其添加到您的pom.xml
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>2.0.1</version>
</dependency>
答案 1 :(得分:0)
你应该尝试fest-assert,它不符合hamcrest,但恕我直言,它优于它(“更流利”)。 例如,日期:
@Test
public void is_between_date_assertions_examples() {
// various usage of isBetween assertion,
// Note that isBetween(2002-12-17, 2002-12-19) includes start date but end date :
assertThat(theTwoTowers.getReleaseDate())
// = 2002-12-18
.isBetween(theFellowshipOfTheRing.getReleaseDate(), theReturnOfTheKing.getReleaseDate())
.isBetween(parse("2002-12-17"), parse("2002-12-19")) // [2002-12-17, 2002-12-19[
.isBetween("2002-12-17", "2002-12-19") // [2002-12-17, 2002-12-19[
.isNotBetween("2002-12-17", "2002-12-18") // [2002-12-17, 2002-12-18[
.isBetween("2002-12-17", "2002-12-18", true, true); // [2002-12-17, 2002-12-18]
}