javajunit5hamcrestassertthat

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.

enter image description here

    @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 ?


Solution

  • 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