Search code examples
javaunit-testingjunithamcrest

Hamcrest assertThat doesnt work when project is moved


I have made a test suite using hamcrest matchers and everything works fine until i move the project on a different machine.

The error i get is:

The method assertThat(T, Matcher<? super T>) in the type MatcherAssert is not applicable  
for the arguments (Object, Matcher<Double>)

Any ideas why i get it and how to fix?

Also the error doesn't appear for all assertThat encounters, some are perceived as correct, even though they have a double for the matcher...


Solution

  • First, you should state the the error you are getting is at compile-time not run-time. The issue is that Double is not a super of Object. So you call assertThat(someObject, someDoubleMatcher) doesn't meet the required signature at compile time. This will however work fine at runtime because the Matcher will check for type.

    Could options...

    // cast expected to object so that created matcher is Matcher<Object>
    assertThat(myObject, CoreMatchers.equalTo((Object)myDouble));
    
    // cast actual value to double so that both value and matcher are Double
    assertThat((Double)myObject, equalTo(myDouble));
    
    // cast Matcher to raw type so generics will be ignored
    assertThat(myObject, (Matcher) equalTo(myDouble));