Search code examples
javajunithamcrest

Is the signature of Hamcrest allOf() wrong?


This matcher checks a collection of matchers and succeeds if they all succeed.

The signature reads:

public static <T> Matcher<T> allOf(Iterable<Matcher<? extends T>> matchers)

Why does this take an iterable of
     Matcher<? extends T>

and not
     Matcher<? super t>

If it's correct as is, please explain.

The type will be inferred from the object being matched (tested), so I would expect the iterable to hold matchers compatible with the object being matched or any superclass thereof. Similarly, allowing the iterable to hold matchers that are only compatible with a subclass seems wrong.


Solution

  • (answering own question)

    My bad - was accidentally using old version. This was fixed in 2007 in Hamcrest 1.2. But the old "extends" version was included in JUnit until JUnit 4.11, released December 2012.