junit5 - JUnit5 - ExpectedException.expectCause() 等效
问题描述
是否有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,它具有非常描述性的异常断言。
推荐阅读
- oracle - 使用 Oracle SQL Developer 导入 .sql 文件?
- unity3d - 在 Unity 3D 中集成 Amazon Chimes
- php - 在 PHP 上嵌套循环时出现非法字符串偏移
- typescript - 将反应减速器转换为打字稿
- php - 多维数组的继续键
- python - Python中csv阅读器的时间复杂度是多少?
- c# - 如何处理 C# 上的 stathreadattribute 错误?
- terraform - 提供者 provider.terraform 不支持资源类型“terraform_remote_state
- curl - weaviate error code 400 parsing body from ...失败的无效字符'G'寻找对象的开头
- php - 通过 Ajax 过滤产品并在 laravel 中分页