Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usingComparator / isEqualTo fails due to Comparator inferred type parameter #2702

Open
scordio opened this issue Jul 8, 2022 · 2 comments
Open
Labels
type: bug A general bug

Comments

@scordio
Copy link
Member

scordio commented Jul 8, 2022

Describe the bug

usingComparator does not work properly with certain combinations of numeric comparators from java.util.Comparator.

  • AssertJ Core version: 3.23.1
  • Java version: 11
  • Test framework version: JUnit 5.8.2

Test case reproducing the bug

This:

assertThat(4L)
  .usingComparator(Comparator.comparingDouble(Number::doubleValue))
  .isEqualTo(4.0d);

and this:

Comparator<Long> comparator = Comparator.comparingDouble(Number::doubleValue);

assertThat(4L)
  .usingComparator(comparator)
  .isEqualTo(4.0d);

fail with:

java.lang.ClassCastException: class java.lang.Double cannot be cast to class java.lang.Long (java.lang.Double and java.lang.Long are in module java.base of loader 'bootstrap')

But this:

Comparator<Number> comparator = Comparator.comparingDouble(Number::doubleValue);

assertThat(4L)
  .usingComparator(comparator)
  .isEqualTo(4.0d);

and this:

Comparator comparator = Comparator.comparingDouble(Number::doubleValue); // raw type warning

assertThat(4L)
  .usingComparator(comparator) // unchecked warning
  .isEqualTo(4.0d);

work properly.

@Michael1993
Copy link

Michael1993 commented Sep 7, 2022

This is the same issue I encountered in this PR (#664). You pass a Comparator<? super Long> to your class but compare two Objects at runtime, making Java assume that your arguments are both Long, leading to a ClassCastException.

@Michael1993
Copy link

I think this can be solved by removing the type arguments of the Comparator passed to usingComparator. This shouldn't lead to any issue (I think?) because it's used to create a ComparatorBasedComparisonStrategy, which uses a rawtype Comparator anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants