I can not compile the hamcrest hasKey () method

This is the code:

Map<Foo, String> map; org.hamcrest.MatcherAssert.assertThat(map, org.hamcrest.Matchers.hasKey(new Foo())); 

This is what the compiler says:

 cannot find symbol method assertThat(java.util.Map<Foo,java.lang.String>, org.hamcrest.Matcher<java.util.Map<Foo,java.lang.Object>>) 

Why and what can I do?

+6
source share
2 answers

I suspect you need something like:

 MatcherAssert.assertThat(map, Matchers.<Foo, String>hasKey()); 

This way you can specify the type of value to call the hasKey method. It looks ugly, though, and I'm a little surprised that type inference doesn't help you ...

+15
source

It looks like you got the same error as me. Is it in Hamcrest> 1.1? They changed the generics in their matches between 1.1 and 1.2. I filed a Hamcrest error here: http://code.google.com/p/hamcrest/issues/detail?id=143

but it turns out that this is actually a compiler error that cannot be fixed in JDK 6 but already fixed in 7: http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=72ce99618021685c3570069c8f60b ? bug_id = 7034548

As John said, there are several ways around this, but they all violate Hamcrest's nice, smooth interface.

+7
source

Source: https://habr.com/ru/post/893823/


All Articles