是否有等效于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"));
}
}
Run Code Online (Sandbox Code Playgroud)
就我个人而言,我更喜欢 AssertJ,它具有非常描述性的异常断言。
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |