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
Introduce assertEquals() variant with equality BiPredicate #2964
Comments
Thanks to the proposal, @jbee. Please note that we do have In line with other team decisions (see #1920 (comment)), we will not include the proposed assertions in JUnit Jupiter. |
In contrast to the explicitly typed proposal in this proposal here offers a generic, functional and smart way to express custom equality with also providing a better expected/actual message for granted. With such a generic method in place future requests to add variant for other types could answered in a generic(!) way. Reopening this issue for a team-discussion. |
Team Decision: We think adding these methods might do more harm than good for the following reasons:
We'll consider making the formatting of assertion failure messages accessible in #2967, though. |
I'll think you missed the point of assertEquals(4d, 4L, BinaryPredicate.compare(Comparator.comparingDouble(Number::doubleValue))); It is comparing a |
Have you considered AssertJ's Considering your last example, this would work: Comparator<Number> comparator = Comparator.comparingDouble(Number::doubleValue);
assertThat(4L)
.usingComparator(comparator)
.isEqualTo(4.0d); Interestingly, this doesn't: assertThat(4L)
.usingComparator(Comparator.comparingDouble(Number::doubleValue))
.isEqualTo(4.0d); I assume something goes wrong with generics signatures so I've raised assertj/assertj#2702. |
Use case: comparing to
BigDecimal
s for equality cannot rely onequals
because mathematical equality is different from it being the exact same value with the same scale and precision.On the other hand using
gives a quite uselss error message:
Even when using custom message
Supplier
we cannot fix the expected/actual output.This is why it would be useful to have:
This could then be used with for example
BigDecimal
:Now the output of expected/actual is proper and no custom message supplier is needed.
Note that choice of
BiPredicate
might just be the best the JDK has to offer.When a dedicated interface would be defined this could just have one type parameter, for example:
I hope it also got clear that this was intentionally suggested as a generic solution that comes in handy in all kinds of use cases. The use case I gave was just one I recently encountered myself that made me think of this.
The text was updated successfully, but these errors were encountered: