alexruiz / fest-assert-2.x

FEST Fluent Assertions 2.x
http://fest.easytesting.org
Apache License 2.0
402 stars 69 forks source link

Java 8 compilation error with generic types #164

Open aramk opened 10 years ago

aramk commented 10 years ago

I've encountered an issue when using Java 1.8 (JDK 8).

import static org.fest.assertions.api.Assertions.assertThat;

public class FestTest {

  public static <T> T someMethod() {
    return (T) "123";
  }

  public static void main(String[] args) {
    assertThat(someMethod()).isEqualTo("123");
  }

}

This runs fine in Java 7 but not Java 8, which gives these compile errors:

java: reference to assertThat is ambiguous
  both method assertThat(java.lang.Boolean) in org.fest.assertions.api.Assertions and method assertThat(java.math.BigDecimal) in org.fest.assertions.api.Assertions match

java: no suitable method found for isEqualTo(java.lang.String)
    method org.fest.assertions.api.AbstractAssert.isEqualTo(java.lang.Boolean) is not applicable
      (argument mismatch; java.lang.String cannot be converted to java.lang.Boolean)
    method org.fest.assertions.api.BooleanAssert.isEqualTo(boolean) is not applicable
      (argument mismatch; java.lang.String cannot be converted to boolean)

The issue is also reported here:

http://openjdk.5641.n7.nabble.com/Re-Strange-covariant-generics-downcasting-issue-with-JDK8-td158942.html

Basically Java 8 is more strict in its generic typing than Java 7 and considered more correct, so the code in https://github.com/alexruiz/fest-assert-2.x/tree/fest-assert-core-2.0M10 is considered invalid by the compiler. Is there any hope of an update resolving this issue? A simple fix to this is to cast the return to an appropriate type:

assertThat((Object) someMethod()).isEqualTo("123");

But this is quite tedious if we have hundreds of such lines in our code with different types.