Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics hell: hamcrest matcher as a method parameter

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?

like image 823
Jan Dudek Avatar asked Oct 21 '12 18:10

Jan Dudek


People also ask

What is Hamcrest matcher?

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.

Can we use Hamcrest with TestNG?

This is great except that TestNG has soft assertions which can't be used from Hamcrest.

Why are there Hamcrest matchers?

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.


2 Answers

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<?>>).

like image 62
newacct Avatar answered Oct 17 '22 01:10

newacct


Anything wrong with this?

public boolean matchIt(final Matcher<? extends Iterable<String>> matcher);
like image 28
Bohemian Avatar answered Oct 17 '22 02:10

Bohemian