JUnit5 - ExpectedException.expectCause() 等效

JUnit5 - ExpectedException.expectCause() equivalent

是否有等同于 ExpectedException.expectCause() (JUnit4) 的 JUnit5? https://junit.org/junit4/javadoc/4.12/org/junit/rules/ExpectedException.html#expectCause(org.hamcrest.Matcher)

这是一个例子:

public class ExpectedExceptionTest {

    @Test
    public void shouldThrow() {
        IOException exc = Assertions.assertThrows(IOException.class, this::throwing);
        Assertions.assertEquals("root cause", exc.getCause().getMessage());
    }

    private void throwing() throws IOException {
        throw new IOException(new IllegalStateException("root cause"));
    }
}

就我个人而言,我更喜欢 AssertJ,它具有很好的描述性 exception assertions

我们将 ExpectedException.expectCause() 替换为:

ThrowableAssert.ThrowingCallable throwingCallable  = () -> someClass.methodWhichThrowAnException(someData);
assertThatThrownBy(throwingCallable).hasCauseInstanceOf(SomeException.class);