<分区>
我想测试 java.time.LocalDate 是否在测试日期的固定天数内,如果可能我想使用 Hamcrest 匹配器。是否有 Hamcrest (java) 的任何匹配器来处理日期?
<分区>
我想测试 java.time.LocalDate 是否在测试日期的固定天数内,如果可能我想使用 Hamcrest 匹配器。是否有 Hamcrest (java) 的任何匹配器来处理日期?
最佳答案
hamcrest有一个日期匹配器扩展库,hamcrest-date,可以匹配LocalDate、LocalDateTime、ZoneDateTime、Date。要比较 LocalDate 是否在测试日期的几天内,您可以使用以下语法:
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));
}
}
可以通过将此依赖项添加到您的 pom 来将库包含在您中。
<dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-date</artifactId>
<version>2.0.1</version>
</dependency>
该项目托管在 github 上 https://github.com/eXparity/hamcrest-date
关于java - Java 8 LocalDate 的 Hamcrest 匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153537/
相关文章:
java - Spring 启动: @PreAuthorize gives 403 forbidden AccessDeniedException
javascript - 使用 Jasmine 测试服务功能 POST 响应
unit-testing - 无法弄清楚如何使用 redux-saga-test-plan 测试 redux-saga 功能
java - Hamcrest 匹配器比较来自 JSON 的 double 值
angularjs - 模拟 e2e 测试时如何检测 API 修改?