Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard Hamcrest matcher to check if collection is empty or null?

Is there a shorter version of the following assert statement using standard Hamcrest matchers?

Collection<Element> collection = ...

assertThat(collection, is(anyOf(nullValue(Collection.class), 
     emptyCollectionOf(Element.class))));

I realize there is a way to create a custom matcher, was hoping that maybe there is already something in place that addresses this without any additional code change.

like image 993
Altair7852 Avatar asked Feb 21 '18 14:02

Altair7852


1 Answers

There is no out-of-the-box solution, and worse, either() can't be used due to this bug. So the shortest way is this:

assertThat(collection, anyOf(nullValue(), empty()));
like image 166
jihor Avatar answered Sep 28 '22 05:09

jihor