How to assertThat something is null with Hamcrest?

前端 未结 4 716
青春惊慌失措
青春惊慌失措 2021-02-01 00:12

How would I assertThat something is null?

for example

 assertThat(attr.getValue(), is(\"\"));

But I get an e

相关标签:
4条回答
  • 2021-02-01 00:24

    You can use IsNull.nullValue() method:

    import static org.hamcrest.Matchers.is;
    import static org.hamcrest.Matchers.nullValue;
    
    assertThat(attr.getValue(), is(nullValue()));
    
    0 讨论(0)
  • 2021-02-01 00:33

    If you want to hamcrest, you can do

    import static org.hamcrest.Matchers.nullValue;
    
    assertThat(attr.getValue(), is(nullValue()));
    

    In Junit you can do

    import static junit.framework.Assert.assertNull;
    assertNull(object);
    
    0 讨论(0)
  • 2021-02-01 00:45

    why not use assertNull(object) / assertNotNull(object) ?

    0 讨论(0)
  • 2021-02-01 00:45

    Use the following (from Hamcrest):

    assertThat(attr.getValue(), is(nullValue()));
    

    In Kotlin is is reserved so use:

    assertThat(attr.getValue(), `is`(nullValue()));
    
    0 讨论(0)
提交回复
热议问题