我正在尝试验证我的所有异常是否正确。因为值包含在 中CompletableFutures
,所以抛出ExecutionException
的异常是我通常会检查的异常。快速示例:
void foo() throws A {
try {
bar();
} catch B b {
throw new A(b);
}
}
所以foo()
翻译抛出的异常bar()
,所有的都是在里面完成的CompletableFutures
(AsyncHandlers
我不会复制整个代码,仅供参考)
在我的单元测试中,我正在bar()
抛出一个异常,并希望在调用时检查它是否正确翻译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")),
));
到目前为止一切顺利,但我也想验证异常的原因A
是异常B
和having(on(A.class).getCause(), CoreMatchers.is(b))
原因CodeGenerationException --> StackOverflowError
TL;DR:我如何获得预期异常的原因?