[英]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.