So, let's have a list of strings and a function that takes a Hamcrest matcher and returns a result of the matches()
method of the provided matcher:
public boolean matchIt(final Matcher<? super List<String>> matcher) {
final List<String> lst = obtainListFromSomewhere();
return matcher.matches(lst);
}
So far so good. Now I can easily call:
matchIt(empty());
matchIt(anything());
matchIt(hasItem("item"));
matchIt(everyItem(equalToIgnoringCase("item")));
...since all of these factory static methods produce a matcher that fits the method signature Matcher<? super List<String>>
.
However, I believe a matcher that accepts an Iterable of objects should be accepted by the matchIt()
method as well:
matchIt(everyItem(anything()));
So I naively changed the matchIt()
method signature:
public boolean matchIt(final Matcher<? super List<? super String>> matcher);
But it doesn't work at all. Not only it doesn't accept everyItem(anything())
, it doesn't even accept the previously correct everyItem(equalToIgnoringCase("item"))
saying (1.7.0_05 compiler version):
actual argument Matcher<Iterable<String>> cannot be converted to Matcher<? super List<? super String>> by method invocation conversion
What the? So what's wrong here? Is it the matchIt()
method signature or is the everyItem()
Hamcrest signature designed wrongly? Or is it just the Java generics system being beyond repair? Thanks a lot for you comments!
EDIT
@rlegendi my intention here is to provide an interface for the client to add and execute predicates about the list. That's the matchIt()
method. Calling matchIt(anything())
makes sense in this scenario, the client wants to know whether the list is anything. Calling matchIt(empty())
means the client wants to know whether the list is empty. Vice versa for matchIt(everyItem(equalToIgnoringCase("item")))
and matchIt(hasItem("item"))
.
My goal here is to have a best matchIt()
method signature possible. The Matcher<? super List<String>>
works fine for all of the previous scenarios. However, I believe the client should be allowed to add Matcher<Iterable<Object>>
matchers as well (for example matchIt(everyItem(notNullValue())
makes perfect sense here, the client wants to know whether every String item of the list is not null).
However I can't find the right signature, matchIt(Matcher<? super List<? super String>>)
doesn't work for everyItem(notNullValue());
I use Hamcrest 1.3.
EDIT 2:
I believe I have found my root misunderstanding.
The everyItem(anything())
expression return an object of type Matcher<Iterable<Object>>
. So I can do easily Matcher<Iterable<Object>> m = everyItem(anything());
However what I don't understand is why I can't do Matcher<? super List<? super String>> m1 = m;
. It seems that Matcher<Iterable<Object>>
is not Matcher<? super List<? super String>>
but I don't get it why.
I can't even do Matcher<? super List<?>> m1 = m;
. Matcher<Iterable<Object>>
is not Matcher<? super List<?>>
? Why?
Hamcrest is a framework that assists writing software tests in the Java programming language. It supports creating customized assertion matchers ('Hamcrest' is an anagram of 'matchers'), allowing match rules to be defined declaratively. These matchers have uses in unit testing frameworks such as JUnit and jMock.
This is great except that TestNG has soft assertions which can't be used from Hamcrest.
Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.
However, I believe a matcher that accepts an Iterable of objects should be accepted by the matchIt() method as well
No, that is not correct. Instead of Iterable
, let's consider List
for the moment. So you have a Matcher<List<Object>>
, and its matches
method takes a List<Object>
. Now, would this take a List<String>
? No. And you probably already know why -- because it could add an object of type Object
into the list.
Now, I know that in naming the class Matcher
, you expect the matches
method to be read-only, and not mutate the list given to it. However, there is no guarantee of that. If indeed it does not add anything to the list, the correct type for the matcher would be Matcher<List<?>>
, which (1) does not allow the matches
method to add anything to the list except null
, and (2) will allow the matches
method to take a list of any type.
I believe that your current method signature public boolean matchIt(final Matcher<? super List<String>> matcher)
already allows Matcher<List<?>>
(or Matcher<Iterable<?>>
).
Anything wrong with this?
public boolean matchIt(final Matcher<? extends Iterable<String>> matcher);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With