AssertThat 在日期比较中的用法
AssertThat usage in a date comparison
我想针对带日期的搜索编写测试。我正在考虑类似
的测试代码
assertThat(repository.findByBookingDateAfter(LocalDate.of(2016, 1, 1))).extracting("bookingDate").are(...));
其中 class 结构类似于以下内容:
public class Booking{
...
LocalDate bookingDate;
...
}
因为查询是在给定日期之后的任何预订,所以我的测试将检查确实晚于该日期的字段。经过一番网上搜索,我没有看到任何有用的信息。我在想我是否在正确的轨道上。
有什么建议吗?
更新:
后来,我更改了 build.gradle 文件中的依赖设置:
testCompile('org.springframework.boot:spring-boot-starter-test'){
exclude group: 'org.assertj'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
获得最新版本的 assertj。
更新二:
下面是任何以"is"开头的方法的截图。 "isAfter" 不在列表中。
我会使用 allMatch
或 allSatisfy
断言来检查每个提取的日期是否都在查询中使用的日期之后。
// set the extracted type to chain LocalDate assertion
assertThat(result).extracting("bookingDate", LocalDate.class)
.allMatch(localDate -> localDate.isAfter(queryDate)));
或
// use a lambda to extract the LocalDate to chain LocalDate assertion
assertThat(result).extracting(Booking::getBookingDate)
.allSatisfy(localDate -> assertThat(localDate).isAfter(queryDate));
第二个断言会得到更好的错误消息,它会显示错误的日期。
我想针对带日期的搜索编写测试。我正在考虑类似
的测试代码assertThat(repository.findByBookingDateAfter(LocalDate.of(2016, 1, 1))).extracting("bookingDate").are(...));
其中 class 结构类似于以下内容:
public class Booking{
...
LocalDate bookingDate;
...
}
因为查询是在给定日期之后的任何预订,所以我的测试将检查确实晚于该日期的字段。经过一番网上搜索,我没有看到任何有用的信息。我在想我是否在正确的轨道上。
有什么建议吗?
更新:
后来,我更改了 build.gradle 文件中的依赖设置:
testCompile('org.springframework.boot:spring-boot-starter-test'){
exclude group: 'org.assertj'
}
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.2'
获得最新版本的 assertj。
更新二:
下面是任何以"is"开头的方法的截图。 "isAfter" 不在列表中。
我会使用 allMatch
或 allSatisfy
断言来检查每个提取的日期是否都在查询中使用的日期之后。
// set the extracted type to chain LocalDate assertion
assertThat(result).extracting("bookingDate", LocalDate.class)
.allMatch(localDate -> localDate.isAfter(queryDate)));
或
// use a lambda to extract the LocalDate to chain LocalDate assertion
assertThat(result).extracting(Booking::getBookingDate)
.allSatisfy(localDate -> assertThat(localDate).isAfter(queryDate));
第二个断言会得到更好的错误消息,它会显示错误的日期。