简体   繁体   English

如何在 assertThat 中使用带有类型推断的 hamcrest nullValue

[英]How to use hamcrest nullValue with type inference in assertThat

I need to write test case using junit 5 and assertThat to check for null value.我需要使用 junit 5 和 assertThat 编写测试用例来检查 null 值。

I have written as below.我写如下。 However, this is giving error with respect to mismatch datatype.但是,这会导致数据类型不匹配的错误。

在此处输入图像描述

    @Test
    void shouldReturnNull() {
        assertThat(OffsetDateTimeConverter.toOffsetDateTime(null), nullValue(OffsetDateTime.class));
    }

Tried this as well.这个也试过了。 However again same error.然而同样的错误。

    @Test
    void shouldReturnNull() {
        assertThat(OffsetDateTimeConverter.toOffsetDateTime(null), is(nullValue(OffsetDateTime.class)));
    }

Any example/suggestion how to use nullValue(T type) to fix this issue?任何示例/建议如何使用 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. I need to write testcase where I can validate if java.sql.Date is correctly converted into java.time.OffsetDateTime using my Converter class. Hence, I have written as below.因此,我写了如下。

@Test
    void toOffsetDateTime() {
        Date date = new Date(System.currentTimeMillis());
        assertThat(date.toLocalDate()).isEqualTo(OffsetDateTimeConverter.toOffsetDateTime(date).toLocalDate());
    }

Above test case is working fine.上面的测试用例工作正常。

I want to know if this is a right approach as I am converting both actual and expected value to local date to compare them?我想知道这是否是正确的方法,因为我将实际值和预期值都转换为本地日期以进行比较?

Seems to me you're using wrong assertThat method.在我看来,您使用了错误的assertThat方法。

You're using assertThat method from assertj where you should be using hamcrest one.您正在使用assertj中的assertThat方法,您应该使用 hamcrest 方法。 For ex: org.hamcrest.MatcherAssert.assertThat例如: org.hamcrest.MatcherAssert.assertThat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM