java - Conflicting overloads for Hamcrest matcher -
the matcher isiterablecontaininginanyorder
has 2 overloads static factory method containsinanyorder
(both have return type matcher<java.lang.iterable<? extends t>>
):
containsinanyorder(java.util.collection<matcher<? super t>> itemmatchers)
containsinanyorder(matcher<? super t>... itemmatchers)
now consider following program:
import static org.hamcrest.collection.isiterablecontaininginanyorder.containsinanyorder; import static org.hamcrest.core.isequal.equalto; import static org.junit.assert.assertthat; import java.util.arrays; import org.junit.test; public class sometest { @suppresswarnings("unchecked") @test public void foo() { assertthat(arrays.aslist("foo","bar"), containsinanyorder(equalto("foo"), equalto("bar"))); } }
when executing junit test, passes, expected. uses second overload of containsinanyorder
shown above.
now, when change assertion (which matches example given in documentation of first overload):
assertthat(arrays.aslist("foo","bar"), containsinanyorder(arrays.aslist(equalto("foo"), equalto("bar")))); ^^^^^^^^^^^^^^
it doesn't compile anymore, because compiler infers return type of containsinanyorder
matcher<iterable<? extends list<matcher<string>>>>
it seems compiler still chooses second overload. if used first one, example should work. why behave this? how can make work?
i using hamcrest 1.3 , oracle java 1.7.
it matches both overloaded methods. i'm not sure why first 1 chosen, can provide hint make choose correct method.
by casting argument collection
:
assertthat(arrays.aslist("foo","bar"), containsinanyorder((collection)arrays.aslist(equalto("foo"), equalto("bar"))));
or specifying generic type t <string>
(don't work static import, though):
assertthat(arrays.aslist("foo","bar"), isiterablecontaininginanyorder.<string>containsinanyorder(arrays.aslist(equalto("foo"), equalto("bar"))));
Comments
Post a Comment