Checking that a List is not empty in Hamcrest

前端 未结 5 1063
忘了有多久
忘了有多久 2020-12-12 19:43

I was wondering if anyone knew of a way to check if a List is empty using assertThat() and Matchers?

Best way I could see just use JUnit:

相关标签:
5条回答
  • 2020-12-12 20:21

    If you're after readable fail messages, you can do without hamcrest by using the usual assertEquals with an empty list:

    assertEquals(new ArrayList<>(0), yourList);
    

    E.g. if you run

    assertEquals(new ArrayList<>(0), Arrays.asList("foo", "bar");
    

    you get

    java.lang.AssertionError
    Expected :[]
    Actual   :[foo, bar]
    
    0 讨论(0)
  • 2020-12-12 20:23

    This works:

    assertThat(list,IsEmptyCollection.empty())
    
    0 讨论(0)
  • 2020-12-12 20:32

    This is fixed in Hamcrest 1.3. The below code compiles and does not generate any warnings:

    // given
    List<String> list = new ArrayList<String>();
    // then
    assertThat(list, is(not(empty())));
    

    But if you have to use older version - instead of bugged empty() you could use:

    hasSize(greaterThan(0))
    (import static org.hamcrest.number.OrderingComparison.greaterThan; or
    import static org.hamcrest.Matchers.greaterThan;)

    Example:

    // given
    List<String> list = new ArrayList<String>();
    // then
    assertThat(list, hasSize(greaterThan(0)));
    

    The most important thing about above solutions is that it does not generate any warnings. The second solution is even more useful if you would like to estimate minimum result size.

    0 讨论(0)
  • 2020-12-12 20:34

    Well there's always

    assertThat(list.isEmpty(), is(false));
    

    ... but I'm guessing that's not quite what you meant :)

    Alternatively:

    assertThat((Collection)list, is(not(empty())));
    

    empty() is a static in the Matchers class. Note the need to cast the list to Collection, thanks to Hamcrest 1.2's wonky generics.

    The following imports can be used with hamcrest 1.3

    import static org.hamcrest.Matchers.empty;
    import static org.hamcrest.core.Is.is;
    import static org.hamcrest.core.IsNot.*;
    
    0 讨论(0)
  • 2020-12-12 20:44

    Create your own custom IsEmpty TypeSafeMatcher:

    Even if the generics problems are fixed in 1.3 the great thing about this method is it works on any class that has an isEmpty() method! Not just Collections!

    For example it will work on String as well!

    /* Matches any class that has an <code>isEmpty()</code> method
     * that returns a <code>boolean</code> */ 
    public class IsEmpty<T> extends TypeSafeMatcher<T>
    {
        @Factory
        public static <T> Matcher<T> empty()
        {
            return new IsEmpty<T>();
        }
    
        @Override
        protected boolean matchesSafely(@Nonnull final T item)
        {
            try { return (boolean) item.getClass().getMethod("isEmpty", (Class<?>[]) null).invoke(item); }
            catch (final NoSuchMethodException e) { return false; }
            catch (final InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); }
        }
    
        @Override
        public void describeTo(@Nonnull final Description description) { description.appendText("is empty"); }
    }
    
    0 讨论(0)
提交回复
热议问题