assertThat((Comparable) obj) not working
See original GitHub issueI 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:
- Created 2 years ago
- Comments:13 (11 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
pros and cons of having
assertThat[InterfaceName]
inAssertionsForInterfaceTypes
: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]
inAssertionsForInterfaceTypes
for commonly implemented interfaces likeComparable
.We’ll discuss it with the team before moving forward.
what about the first suggestion of adding
assert[InterfaceName]
? these methods don’t have to be added inAssertionsForClassTypes
/AssertionsForInterfaceTypes
if you want to prevent it from having too many methods; it is also fine in each Interface implementation likeGenericComparableAssert
like that, we can still import the methods statically (you can do these also additionally to the other plan you presented above)