Mockito’s Matcher vs Hamcrest Matcher?

Hamcrest matcher methods return Matcher<T> and Mockito matchers return T. So, for example: org.hamcrest.Matchers.any(Integer.class) returns an instance of org.hamcrest.Matcher<Integer>, and org.mockito.Matchers.any(Integer.class) returns an instance of Integer. That means that you can only use Hamcrest matchers when a Matcher<?> object is expected in the signature – typically, in assertThat calls. When setting up expectations or verifications … Read more

Mockito and Hamcrest: how to verify invocation of Collection argument?

You can just write verify(service).perform((Collection<String>) Matchers.argThat(contains(“a”, “b”))); From the compiler’s point of view, this is casting an Iterable<String> to a Collection<String> which is fine, because the latter is a subtype of the former. At run time, argThat will return null, so that can be passed to perform without a ClassCastException. The important point about it … Read more

How to check if collection contains items in given order using Hamcrest

You can use contains matcher instead, but you probably need to use latest version of Hamcrest. That method checks the order. assertThat(list, contains(“foo”, “boo”)); You can also try using containsInAnyOrder if order does not matter to you. That’s the code for contains matcher: public static <E> Matcher<Iterable<? extends E>> contains(List<Matcher<? super E>> itemMatchers) { return … Read more

Why doesn’t this code attempting to use Hamcrest’s hasItems compile?

Just ran into this post trying to fix it for myself. Gave me just enough information to work it out. You can give the compiler just enough to persuade it to compile by casting the return value from hasItems to a (raw) Matcher, eg: ArrayList<Integer> actual = new ArrayList<Integer>(); ArrayList<Integer> expected = new ArrayList<Integer>(); actual.add(1); … Read more

Map equality using Hamcrest

The shortest way I’ve come up with is two statements: assertThat( affA.entrySet(), everyItem(isIn(affB.entrySet()))); assertThat( affB.entrySet(), everyItem(isIn(affA.entrySet()))); But you can probably also do: assertThat(affA.entrySet(), equalTo(affB.entrySet())); depending on the implementations of the maps, and sacrificing the clarity of the difference report: that would just tell you that there is a difference, while the statement above would also … Read more

Hamcrest number comparison using between

An alternative to Jeff’s solution is to use both: assertThat(50L, is(both(greaterThan(12L)).and(lessThan(1658L)))); I think that’s quite readable. You also get a good error message in case the check failed: Expected: is (a value greater than <50L> and a value less than <1658L>) got: <50L>

How do I use Hamcrest with JUnit 5 when JUnit 5 doesn’t have an assertThat() function?

You have to make sure Hamcrest is included in the classpath and then use the assertThat() function provided by Hamcrest. From the current JUnit 5 User Guide – Writing Tests Assertions, JUnit Jupiter’s org.junit.jupiter.Assertions class does not provide an assertThat() method like the one found in JUnit 4’s org.junit.Assert class which accepts a Hamcrest Matcher. … Read more