assert that a list is not empty in JUnit

This reads quite nicely and uses Hamcrest. Exactly what you asked for ;) Always nice when the code reads like a comment.

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

You can add is as a static import to your IDE as I know that eclipse and IntelliJ is struggling with suggesting it even when it is on the classpath.


IntelliJ

Settings -> Code Style -> Java -> Imports 

Eclipse

Prefs -> Java -> Editor -> Content Assist -> Favourites 

And the import itself is import static org.hamcrest.CoreMatchers.is;


I like to use

Assert.assertEquals(List.of(), result)

That way, you get a really good error message if the list isn't empty. E.g.

java.lang.AssertionError: 
Expected :[]
Actual   :[something unexpected]

You can simply use

assertFalse(result.isEmpty());

Regarding your problem, it's simply caused by the fact that you forgot to statically import the is() method from Hamcrest;

import static org.hamcrest.CoreMatchers.is;

Tags:

Junit

Junit4