Fre*_*eit 17
如果你想测试一个特定的例外是不是在一个条件下抛出其他异常可能会被抛出,试试这个:
try {
myMethod();
}
catch (ExceptionNotToThrow entt){
fail("WHOOPS! Threw ExceptionNotToThrow" + entt.toString);
}
catch (Throwable t){
//do nothing since other exceptions are OK
}
assertTrue(somethingElse);
//done!
Run Code Online (Sandbox Code Playgroud)
您可以使用assertj执行以下操作
如果你想检查异常是否没有抛出那么
Throwable throwable = catchThrowable(() -> sut.method());
assertThat(throwable).isNull();
Run Code Online (Sandbox Code Playgroud)
或者你希望抛出
Throwable throwable = catchThrowable(() -> sut.method());
assertThat(throwable).isInstanceOf(ClassOfExecption.class)
.hasMessageContaining("expected message");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18739 次 |
| 最近记录: |