10

I have Map declared as following:

Map<String, Object> data

I put a String in it and verify its value like this:

assertEquals("value", data.get("key"));

Now, I'd like to rewrite the verification to use assertThat instead of assertEquals. I've tried the following:

assertThat(data.get("key"), equalTo("value"));

And of course it didn't work because of type mismatch:

Wrong 2nd argument type. Found: 'org.hamcrest.Matcher<java.lang.String>', required: 'org.hamcrest.Matcher<? super java.lang.Object>' less...

Explicit type cast of the first argument to String helps, but I'd like to avoid it. For example assertEquals doesn't require type cast. So, how can I check that the value, which was put into Map object, declared above, is equal to particular String, using the assertThat method?

3
  • casting the data.get to a String might help Commented Oct 10, 2016 at 10:35
  • Yes, it helps. The point is that I want to avoid explicit type cast. Looking for a way to make it work like assertEquals but with assertThat. Commented Oct 10, 2016 at 10:44
  • Matcher.<Object>equalTo("value"). Commented Oct 10, 2016 at 10:47

2 Answers 2

6

The "more assertThat" way of doing things would be:

Map<String, Object> expectedData = Collections.singletonMap("key", "value");

asssertThat(data, is(expectedData));

Please note:

  • Maybe you need type hints for the call to singletonMap
  • Besides the is matcher, there are other matchers that would allow you to check that data contains your "expected" map data

For your specific problem: that is caused because how generics come into play here; it might be sufficient to use (String) data.get("key") - to tell the compiler that the "actual" argument is of type String.

In the end - I have no idea what your problem is. I wrote down this piece of code:

public void test() {
    Map<String, Object> data = new HashMap<>();
    data.put("key", "value");
    assertThat(data.get("key"), is("value"));

    Map<String, Object> expectedData = Collections.singletonMap("key", "value");
    assertThat(data, is(expectedData));
}

It compiles fine, and the unit test runs and passes. In other words: actually I am unable to repro your problem.

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately "is" has the same issue as "equalsTo". It looks like I can't achieve what I want without explicit type cast to String.
Please see my updated answer. Things work for me (both ways) without any casting (but to be precise: using Java8). In Java7, type inference might give you a problem; requiring you to put down that cast.
Hmm, checked again exactly your example and experienced the same issue as before. So it definitely looks like version mismatch of anything. I'll continue researching and checking.
Lets be precise about wording: there is no "version" mismatch. What I am saying is: my code works fine with Java8. When you are using an older version from Java, the type inference works less good; therefore older Java might only work with hardcoded casts.
Yes, that what I meant. Sorry, if it confused you. At least your comment shows that the desired approach is working.
3

try this

assertThat(data.get("key"), equalTo("value"))

or

    assertThat(data.get("key"), CoreMatchers.equalTo("value"))

1 Comment

Welcome to Stack Overflow! Please read what this site is about and "How to answer" before answering a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.