arashja / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Compiler error when using assertThat and polymorphism #31

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I've been finding that the current definition of assertThat() gives
compiler errors when using polymorphism:

    public void testWibble() {
        Circle2D circle = new Circle2D(0,0,1);
        Shape circleAsShape = circle;

        assertThat(circleAsShape, equalTo(circle));          // ERROR!
        assertThat(circle,        equalTo(circleAsShape));
    }

I believe the following signature for assertThat() solves the problem:
  public static <T> void assertThat(T actual, Matcher<? extends T> matcher)

Also, given that the Matcher<T> interface doesn't actually use the 'T'
parameter, is there any reason for it having a generic type and that type
being used in assertThat()?

Original issue reported on code.google.com by i.am.pau...@googlemail.com on 3 Mar 2008 at 2:59

GoogleCodeExporter commented 8 years ago
The correct signature is:

void <T> assertThat(T actual, Matcher<? super T> matcher)

The generic type parameter is used for type inference in APIs that use 
Hamcrest, such
as jMock.

Original comment by nat.pr...@gmail.com on 3 Mar 2008 at 11:07

GoogleCodeExporter commented 8 years ago
Fix committed to SVN

Original comment by nat.pr...@gmail.com on 4 Mar 2008 at 8:52

GoogleCodeExporter commented 8 years ago

Original comment by smgfree...@gmail.com on 22 Nov 2008 at 12:18