我正在尝试验证所有异常是否正确。由于值被包装在 CompletableFuture
中,因此抛出的异常是带有原因的 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: 我如何获得预期异常的原因的原因?