Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).hasCauseInstanceOf(Exception.class)
.hasStackTraceContaining("no way")
.hasStackTraceContaining("you shall not pass");
Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).getCause()
.hasMessage("you shall not pass");
getRootCause():
Throwable rootCause = new RuntimeException("go back to the shadow!");
Throwable cause = new Exception("you shall not pass", rootCause);
Throwable runtime = new RuntimeException("no way", cause);
assertThat(runtime).getRootCause()
.hasMessage("go back to the shadow!");
Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).extracting(Throwable::getCause, as(THROWABLE))
.hasMessage("you shall not pass");
2条答案
按热度按时间p5cysglq1#
不完全是这样,目前最好的方法是使用
hasStackTraceContaining
,例如pu82cl6c2#
自AssertJ 3.16起,提供了两个新选项:
getCause()
:getRootCause()
:自AssertJ 3.14起,
extracting
可以与InstanceOfAssertFactory
一起使用:as()
是从org.assertj.core.api.Assertions
静态导入的,THROWABLE
是从org.assertj.core.api.InstanceOfAssertFactories
静态导入的。