I'm trying to verify that all my exceptions are correct. Because values are wrapped in CompletableFutures
, the exception thrown is ExecutionException
with cause being the exception that I would normally check. Quick example:
void foo() throws A {
try {
bar();
} catch B b {
throw new A(b);
}
}
So foo()
translates exception thrown by bar()
, and all of that is done inside CompletableFutures
and AsyncHandlers
(I won't copy the whole code, it's just for reference)
In my unit tests I'm making bar()
throw an exception and want to check that it's translated correctly when calling foo()
:
Throwable b = B("bleh");
when(mock.bar()).thenThrow(b);
ExpectedException thrown = ExpectedException.none();
thrown.expect(ExecutionException.class);
thrown.expectCause(Matchers.allOf(
instanceOf(A.class),
having(on(A.class).getMessage(),
CoreMatchers.is("some message here")),
));
So far so good, but I also want to verify that cause of exception A
is exception B
and having(on(A.class).getCause(), CoreMatchers.is(b))
causes CodeGenerationException --> StackOverflowError
TL;DR: How do I get cause of cause of expected exception?
Maybe you should try with the simple hasProperty Matcher, in order to isolate the problem:
thrown.expectCause(allOf(
instanceOf(A.class),
hasProperty("message", is("some message here")),
));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With