I'd like to test if a java.time.LocalDate is within a fixed number of days of a test date and I'd like to use Hamcrest matchers if possible. Are there any matchers for Hamcrest (java) for working with Dates?
Hamcrest Matchers for Java 8 LocalDate [closed]
Asked Answered
There is a date matcher extension library for hamcrest, hamcrest-date, which can match LocalDate, LocalDateTime, ZoneDateTime, and Date. To compare if a LocalDate is within a number of days of a test date you can use this syntax:
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import org.exparity.hamcrest.date.LocalDateMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
public class LocalDateTest {
@Test
public void isDate() {
LocalDate actual = LocalDate.now();
LocalDate expected = LocalDate.of(2015, Month.OCTOBER, 15);
MatcherAssert.assertThat(actual,
LocalDateMatchers.within(5, ChronoUnit.DAYS, expected));
}
}
The library can be included in you by adding this dependency to your pom.
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>2.0.1</version>
</dependency>
The project is hosted on github at https://github.com/eXparity/hamcrest-date
Can you merge your code into hamcrest ? –
Collation
© 2022 - 2024 — McMap. All rights reserved.