AssertJ 3.23
cause()
is favored over getCause()
:
Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).cause()
.hasMessage("you shall not pass");
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).rootCause()
.hasMessage("go back to the shadow!");
AssertJ 3.16
Two new options are available:
Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).getCause()
.hasMessage("you shall not pass");
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!");
AssertJ 3.14
extracting
with InstanceOfAssertFactory
could be used:
Throwable runtime = new RuntimeException("no way",
new Exception("you shall not pass"));
assertThat(runtime).extracting(Throwable::getCause, as(THROWABLE))
.hasMessage("you shall not pass");
as()
is statically imported from org.assertj.core.api.Assertions
and THROWABLE
is statically imported from org.assertj.core.api.InstanceOfAssertFactories
.
getCause()
if your version is >= 3.16 as suggested by Stefano Cordio – Swirsky