Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertj verify that a field of each items in a collection is always null

Tags:

java

assertj

I'm looking for a solution to check that each items in a collection have the field expectedNullField null.

The following doesn't work:

assertThat(aCollection).extracting("expectedNullField").isNull();

Note that the following works as expected: assertThat(aCollection).extracting("expectedNotNullField").isNotNull();

Anybody to help me ?

Thanks.

like image 686
pierrefevrier Avatar asked Mar 09 '17 10:03

pierrefevrier


1 Answers

If you know the size (let's say it is 3) you can use

assertThat(aCollection).extracting("expectedNullField")
                       .containsOnly(null, null, null);

or if you are only interested in checking that there is a null value

assertThat(aCollection).extracting("expectedNullField")
                       .containsNull();

Note that you can't use:

    assertThat(aCollection).extracting("expectedNullField")
                           .containsOnly(null);

because it is ambiguous (containsOnly specifying a varargs params).

I might consider adding containsOnlyNullElements() in AssertJ to overcome the compiler error above.

like image 181
Joel Costigliola Avatar answered Sep 21 '22 20:09

Joel Costigliola