question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

assertThat((Comparable) obj) not working

See original GitHub issue

I have a class (PowerClass in examples below) which is Iterable and Comparable. I was trying to test it with something similar like the example below.

// assertThat(new PowerClass()).isEqualTo(new PowerClass()); // Ambiguous method call.
assertThat((Object) new PowerClass()).isEqualTo(new PowerClass()); // does not look nice
assertThatObject((Object) new PowerClass()).isEqualTo(new PowerClass()); // better way of line above
assertThat((Iterable<PowerClass>) new PowerClass()).contains(new PowerClass()); // looks not nice but works
// assertThat((Comparable<PowerClass>) new PowerClass()).isGreaterThan(new PowerClass()); // counts as Object not Comparable

It works only like expected if the class is only Iterable and no cast is present.

assertThat(new PowerClass()).isGreaterThan(new PowerClass());

It would be nice if the cast would work, and to prevent ambiguous method calls to have own assertThat calls for Iterable and Comparable like assertThatObject. (ref. #1388) The other solution would be to call the GenericComparableAssert constructor on our own (or create a method for that).

This may also affect other interfaces (like Throwable?).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
joel-costigliolacommented, Mar 9, 2022

pros and cons of having assertThat[InterfaceName] in AssertionsForInterfaceTypes:

  • pros: easy to discover
  • cons: quite a lot of new methods in AssertionsForInterfaceTypes and co (BDDAssertions, WithAssertions, …)

pros and cons of having assertThat[InterfaceName] in [InterfaceName]Assert is basically the opposite.

maybe we can find a middle ground where we would add assertThat[InterfaceName] in AssertionsForInterfaceTypes for commonly implemented interfaces like Comparable.

We’ll discuss it with the team before moving forward.

1reaction
rala72commented, Mar 8, 2022

note that one will not be able to static import every potentially ambiguous assertThat

what about the first suggestion of adding assert[InterfaceName]? these methods don’t have to be added in AssertionsForClassTypes / AssertionsForInterfaceTypes if you want to prevent it from having too many methods; it is also fine in each Interface implementation like GenericComparableAssert like that, we can still import the methods statically (you can do these also additionally to the other plan you presented above)

Read more comments on GitHub >

github_iconTop Results From Across the Web

assertThat((Comparable) obj) not working #2504 - GitHub
I have a class (PowerClass in examples below) which is Iterable and Comparable. I was trying to test it with something similar like...
Read more >
Test two instances of object are equal JUnit - Stack Overflow
I need to assert that two object instances A and B are equal in value and not actual object. We have a method...
Read more >
AssertJ / Fluent assertions for java
We want to start typing asser and let code completion suggest assertThat from AssertJ (and not the one from Hamcrest !). Eclipse configuration....
Read more >
How to Compare Object Instances in your Unit Tests Quickly ...
It might not be feasible to manually compare EVERY field with expected values in another object. Here's xUnit's Assert.Equal<T>(T expected, T actual) method:...
Read more >
Matchers (Hamcrest)
org.hamcrest ... Creates a matcher of Comparable object that matches when the examined object is equal to ... assertThat(cheese, is(not(equalTo(smelly)))).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found