yegor256
yegor256

Reputation: 105053

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?

Upvotes: 6

Views: 2834

Answers (2)

Ryan Stewart
Ryan Stewart

Reputation: 128799

It sounds like you've hit the same bug that I did. Is this in Hamcrest > 1.1? They changed the generics on their matchers between 1.1 and 1.2. I filed a Hamcrest bug here: http://code.google.com/p/hamcrest/issues/detail?id=143

but it turns out that this is actually a bug in the compiler which can't be fixed in JDK 6 but is already fixed in 7: https://bugs.java.com/bugdatabase/view_bug;jsessionid=72ce99618021685c3570069c8f60b?bug_id=7034548

As Jon mentioned, there are a couple of ways to work around it, but they all break the nice, fluent interface of Hamcrest.

Upvotes: 7

Jon Skeet
Jon Skeet

Reputation: 1500165

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

Upvotes: 15

Related Questions