简体   繁体   中英

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.

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?

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.

You're using assertThat method from assertj where you should be using hamcrest one. For ex: org.hamcrest.MatcherAssert.assertThat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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