Is there a Hamcrest matcher to check that a Collection is neither empty nor null?
Asked Answered
B

3

20

Is there a Hamcrest matcher which checks that the argument is neither an empty Collection nor null?

I guess I could always use

both(notNullValue()).and(not(hasSize(0))

but I was wondering whether there is a simpler way and I missed it.

Border answered 15/12, 2014 at 18:30 Comment(4)
That looks pretty simple to me. Also, it's important that your tests express their intent with as much clarity as possible, and that code is very readable.Gastropod
I don't really know hamcrest, but logically you could check for size >= 0, if the API supports such calls.Epp
this question's title asks the opposite of this question's body. To answer the question's title: assertThat( metadata, either( is( empty() ) ).or( is( nullValue() ) ) );Defrost
Not an exact match to your question, but you can assertTrue(CollectionUtils.isNotEmpty(collectioin)). CollectionUtils is an Apache Commons Lang class.Defective
P
13

You can combine the IsCollectionWithSize and the OrderingComparison matcher:

@Test
public void test() throws Exception {
    Collection<String> collection = ...;
    assertThat(collection, hasSize(greaterThan(0)));
}
  • For collection = null you get

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: was null
    
  • For collection = Collections.emptyList() you get

    java.lang.AssertionError: 
    Expected: a collection with size a value greater than <0>
        but: collection size <0> was equal to <0>
    
  • For collection = Collections.singletonList("Hello world") the test passes.

Edit:

Just noticed that the following approch is not working:

assertThat(collection, is(not(empty())));

The more i think about it the more i would recommend a slightly altered version of the statement written by the OP if you want to test explicitly for null.

assertThat(collection, both(not(empty())).and(notNullValue()));
Plotinus answered 16/12, 2014 at 7:6 Comment(2)
I accepted your answer because I think that both ways shown here, hasSize(greaterThan(0)) and both(not(empty())).and(notNullValue()) are a good way.Border
Any reason why I get 1 expectation failed. JSON path images doesn't match. Expected: (an empty collection or null) Actual: null when i try to check for empty collection or null with your code : myResponse.body(myArrayAttr, either(empty()).or(nullValue()));Roshan
E
2

As I posted in the comments, the logical equivalent of collection != null and size != 0 is size > 0, that implies the collection is not null. A simpler way to express size > 0 is there is an (arbitrary) element X in collection. Below a working code example.

import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.CoreMatchers.anything;

public class Main {

    public static void main(String[] args) {
        boolean result = hasItem(anything()).matches(null);
        System.out.println(result); // false for null

        result = hasItem(anything()).matches(Arrays.asList());
        System.out.println(result); // false for empty

        result = hasItem(anything()).matches(Arrays.asList(1, 2));
        System.out.println(result); // true for (non-null and) non-empty 
    }
}
Epp answered 15/12, 2014 at 22:52 Comment(0)
R
2

You are welcome to use Matchers:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;

assertThat(collection, anyOf(nullValue(), empty()));
Reckford answered 17/4, 2019 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.