[英]How to use hamcrest nullValue with type inference in assertThat
我需要使用 junit 5 和 assertThat 编写测试用例来检查 null 值。
我写如下。 但是,这会导致数据类型不匹配的错误。
@Test
void shouldReturnNull() {
assertThat(OffsetDateTimeConverter.toOffsetDateTime(null), nullValue(OffsetDateTime.class));
}
这个也试过了。 然而同样的错误。
@Test
void shouldReturnNull() {
assertThat(OffsetDateTimeConverter.toOffsetDateTime(null), is(nullValue(OffsetDateTime.class)));
}
任何示例/建议如何使用 nullValue(T type) 来解决此问题?
I need to write testcase where I can validate if java.sql.Date is correctly converted into java.time.OffsetDateTime using my Converter class. 因此,我写了如下。
@Test
void toOffsetDateTime() {
Date date = new Date(System.currentTimeMillis());
assertThat(date.toLocalDate()).isEqualTo(OffsetDateTimeConverter.toOffsetDateTime(date).toLocalDate());
}
上面的测试用例工作正常。
我想知道这是否是正确的方法,因为我将实际值和预期值都转换为本地日期以进行比较?
在我看来,您使用了错误的assertThat
方法。
您正在使用assertj
中的assertThat
方法,您应该使用 hamcrest 方法。 例如: org.hamcrest.MatcherAssert.assertThat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.