Checking that a List is not empty in Hamcrest

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 … Read more

Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

There’s no big advantage for those cases where an assertFoo exists that exactly matches your intent. In those cases they behave almost the same. But when you come to checks that are somewhat more complex, then the advantage becomes more visible: val foo = List.of(“someValue”); assertTrue(foo.contains(“someValue”) && foo.contains(“anotherValue”)); Expected: is <true> but: was <false> vs. … Read more

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

Make sure the hamcrest jar is higher on the import order than your JUnit jar. JUnit comes with its own org.hamcrest.Matcher class that is probably being used instead. You can also download and use the junit-dep-4.10.jar instead which is JUnit without the hamcrest classes. mockito also has the hamcrest classes in it as well, so … Read more