我正在尝试为具有Iterable字段的类编写自定义匹配器.我无法找到一种方法来使其可以接受任何匹配项everyItem,hasItem和contains-因为每个匹配项都返回略有不同的泛型类型.做这个的最好方式是什么?
这是一个简单的示例来演示该问题-如何进行编译?我正在使用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>>
}
}
解决方法:
以下签名对我有用:
public static Matcher<Band> hasMembers(final Matcher<? super Iterable<Member>> matcher) {
// ...
}
边注:
Hamcrest具有FeatureMatcher
类,可以轻松为特定属性创建匹配器:
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();
}
};
};