7

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

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • It won't be a conflict if you only have one copy of hamcrest. Which versions of JUnit and Hamcrest do you have? Remember, later versions of JUnit include their own copy of bits of Hamcrest. – skaffman Nov 30 '09 at 22:12
  • I recently added hamcrest, after only using jUnit before that. It's possible the hamcrest that's bundled with jUnit is older, and should be updated. – ripper234 Dec 01 '09 at 12:16

4 Answers4

5

I believe the problem is that JUnit comes bundled with an older copy of Hamcrest (1.1) as signatures in later version of Hamcrest are incompatible with JUnit. There are two possible solutions:

  1. Drop your version of Hamcrest (1.3) from the classpath, and use the copy bundled with JUnit.
  2. Use a different release version of JUnit (I believe the jars are named like 'junit-dep-xxx.jar) which does not include Hamcrest
  3. Change calls of org.junit.Assert.assertThat() to org.hamcrest.MatcherAssert.assertThat()`.

The latter is probably my recommended option, since the Hamcrest version of assertThat() produces nicer failure messages, and versions later than 1.1 have some nice features (e.g. TypeSafeDiagnosingMatcher).

Grundlefleck
  • 124,925
  • 25
  • 94
  • 111
2

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
  • 35,575
  • 15
  • 95
  • 119
  • 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 Nov 30 '09 at 18:03
  • 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 Dolk Nov 30 '09 at 18:12
  • 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 Nov 30 '09 at 19:25
  • 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 Nov 30 '09 at 20:55
  • 1
    Not sure, but most likely it's for JUnit 3 backward compatibility. – sfussenegger Dec 01 '09 at 08:52
1

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
  • 42,793
  • 6
  • 62
  • 96
0

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
  • 728
  • 1
  • 4
  • 14