I am trying to write a custom matcher for a class that has an Iterable field. I can't work out a way to do it so that it can accept any of the matchers everyItem, hasItem, and contains - because each of those returns a slightly different generic type. What is the best way to do this?
Here is a simple example to demonstrate the problem - how can I make it compile? I am using Hamcrest 1.3.
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import java.util.Collection;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
public class MatcherExample {
public static class Band {
public Collection<Member> getMembers() {
return null;
}
};
public static class Member {
};
public static Matcher<Band> hasMembers(final Matcher<Iterable<Member>> matcher) {
return new TypeSafeDiagnosingMatcher<Band>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(Band item, Description mismatchDescription) {
return matcher.matches(item.getMembers());
}
};
}
public static void main(String[] args) {
Band band = new Band();
assertThat(band, hasMembers(everyItem(equalTo(new Member())))); // works with signature Matcher<Iterable<Member>>
assertThat(band, hasMembers(hasItem(equalTo(new Member())))); // works with signature Matcher<Iterable<? super Member>>
assertThat(band, hasMembers(contains(equalTo(new Member())))); // works with signature Matcher<Iterable<? extends Member>>
}
}
The following signature is working for me:
public static Matcher<Band> hasMembers(final Matcher<? super Iterable<Member>> matcher) {
// ...
}
Side note:
Hamcrest has the FeatureMatcher
class to easily create matchers for a specific property:
public static Matcher<Band> hasMembers(final Matcher<? super Iterable<Member>> matcher) {
return new FeatureMatcher<Band, Iterable<Member>>(matcher, "a band with members", "members") {
@Override
protected Iterable<Member> featureValueOf(Band actual) {
return actual.getMembers();
}
};
};