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
Assert return value of callable throwing exception #1652
Comments
How about... assertThatCode(() -> ...)
.doesNotThrowAnyExceptionAndReturnsSatisfying(Consumer<? super T> consumer)
;
assertThatCode(() -> ...)
.doesNotThrowAnyExceptionAndReturnsSatisfying(IntConsumer consumer)
; ? |
sorry this one fell under our radar, we'll look at it for 3.22.0. |
I don't think it is technically possible in the current codebase to change
At the moment IMO the best approach would be to let the test fail with the exception: @Test
public void otherSolution() {
int x = doStuff();
assertThat(x).isEqualTo(3);
} I know the test won't fail with an assertion error but with the exception from I'm still thinking we could something like though: // chains Object assertion
assertThatCodeReturnedValue(() -> doStuff()).isEqualTo(3);
// use asInstanceOf to chain specific typed assertion:
assertThatCodeReturnedValue(() -> doStuff()).asInstanceOf(INTEGER).isLessThan(10); WDYT? |
Better naming: // chains Object assertion
assertThatValueReturnedBy(() -> doStuff()).isEqualTo(3);
// use asInstanceOf to chain specific typed assertion:
assertThatValueReturnedBy(() -> doStuff()).asInstanceOf(INTEGER).isLessThan(10); |
I think it would be possible to add an overloaded version of assertThatCode(() -> "asd".isEmpty()) // executes Callable::call storing either the result or the thrown exception
.doesNotThrowAnyException()
.result() // returns AbstractObjectAssert
.isEqualTo(42); still keeping compatibility with everything else which is not a assertThatCode(() -> System.out.println("")) // AbstractThrowableAssert
.doesNotThrowAnyException(); I'll prepare a draft PR for evaluation. |
Hi @scordio. Is this in progress? Thanks. |
Summary
It would be nice if the
assertThatCode().doesNotThrowException()
API would be able execute functions that return a value and then return that value for further inspection.This would make it easy to provide a nice description to an otherwise maybe nondescript exception that is thrown.
Example
The text was updated successfully, but these errors were encountered: