3
我想测试java.time.LocalDate是否在测试日期的固定天数内,如果可能,我想使用Hamcrest匹配器。 Hamcrest(java)有没有与日期合作的匹配器?Hamcrest Matchers for Java 8 LocalDate
我想测试java.time.LocalDate是否在测试日期的固定天数内,如果可能,我想使用Hamcrest匹配器。 Hamcrest(java)有没有与日期合作的匹配器?Hamcrest Matchers for Java 8 LocalDate
有一个可以匹配LocalDate,LocalDateTime,ZoneDateTime和Date的hamcrest日期匹配器扩展库,hamcrest-date。为了比较,如果是LOCALDATE一些测试日期的天之内你可以使用这个语法:加入这个依赖于你的POM
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));
}
}
库可以被包含在你的。
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>2.0.1</version>
</dependency>
在GitHub上托管