2015-10-15 56 views

回答

6

有一个可以匹配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> 

该项目在https://github.com/eXparity/hamcrest-date

在GitHub上托管