Search code examples
javagenericsjunithamcrest

I can't compile 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 compiler is saying:

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?


Solution

  • I suspect you need something like:

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

    That way you can specify the value type for the hasKey method call. Looks butt-ugly though, and I'm slightly surprised that type inference doesn't help you...