KentBeck/junit — Issue #378: Request to change the generics definition of Assert.assertThat
Author: llae
Created: 2012-02-12 07:03:41+00:00
State: open

Here is the current signature of Assert.assertThat:

public static void assertThat(T actual, Matcher matcher)

Which mean you'll get a compilation error if you try the following:

CharSequence actual = "hello";
String expected = "hello";
assertThat(actual, is(expected));

That failed because `expected` (String) is a subtype of `actual` (CharSequence), which makes the generic requirement on the matcher parameter `` to fail.

So here I'm suggesting a solution, change the generics definition of `Assert.assertThat` to something like:

public static void assertThat(A actual, Matcher expected)

Using this with the previous example, you get T = CharSequence, A = CharSequence, E = String, and since both CharSequence and String are subtypes of CharSequence, your program will compile fine (just like the old assertEquals).

I've also raised the same issue with hamcrest: http://code.google.com/p/hamcrest/issues/detail?id=176