views:

357

answers:

4

Trying to compile this code

import static org.hamcrest.Matchers.is;
import static org.hamcrest.number.OrderingComparison.lessThan;

...

Assert.assertThat(0, is(lessThan(1)));

issues this compilation error:

assertThat(Object, org.hamcrest.Matcher<java.lang.Object>) cannot be applied to (int, org.hamcrest.Matcher<capture<? super java.lang.Integer>>)

Could be this collisions between different hamcrest versions? I'm using jUnit 4.6 and hamcrest 1.3

+1  A: 

I don't use Hamcrest, but obviously int isn't an Object. Use Integer instead, e.g.

Assert.assertThat(Integer.valueOf(0), is(lessThan(1)));

I suppose you are using Java version <= 1.4 where auto-boxing doesn't work. Hence you need an explicit conversion to Integer first.

sfussenegger
Java 1.6, and adding casts to Integer doesn't solve anything. Oh, and thanks for the downvote for a legitimate question (assuming it was you, if not I take it back).
ripper234
I don't think that someone provides an answer and downvotes the question. Guess it was somebody else. +1 from me, just because I don't understand the uncommented downvote too.
Andreas_D
I've just tried with JDK 6, hamcrest-all-1.2.jar and junit-4.7.jar and it compiles just fine. Pay attention to use org.junit.Assert, not junit.framework.Assert. And be careful with insulting people that are simply trying to help you, even if they downvoted (which I didn't).
sfussenegger
Sorry about my false assumption - I will make sure I'm using the correct Assert. What's the difference between them anyway - why are they both in the classpath?
ripper234
Didn't help, sorry.
ripper234
Not sure, but most likely it's for JUnit 3 backward compatibility.
sfussenegger
A: 

It is a very strange problem. I think we need some more information, since it should work correctly. I tried to reproduce it using JUnit 4.4 and Hamcrest 1.1 (a bit older, but that's what I'm using at my current project, so it was easy to test) and it worked perfectly.

The only difference I noticed is that my Eclipse imported org.hamcrest.Matchers.lessThan instead of org.hamcrest.number.OrderingComparisons.lessThan, but when I used the latter it worked flawlessly as well.

It could be caused by the fact that you using an old version of Hamcrest or JUnit (which versions are you actually using? You didn't mentioned it yet). What is strange is the fact that you got an error even when you've added an explicit cast to Integer. That's interesting, and it could be helpful when you post this error...

Anyway, it should work perfectly since there are no syntax errors or something, so your setup has to be the cause of the problem.

Martin Sturm
I have junit 4.6 and hamcrest 1.3
ripper234
+1  A: 

I think perhaps the problem is your assertThat method. If it says,

void assertThat(Object item, Matcher<Object> matcher) { ... }

then you need to change it to:

void <T> assertThat(T item, Matcher<? super T> matcher) { ... }

Maybe your JUnit library is out of date compared to your Hamcrest library? Did you build them both yourself? Do you possibly have multiple copies of JUnit or Hamcrest in your classpath?

Jason Orendorff