Java – assertj asserts on the cause message
•
Java
Is there any way to throw a method again when using assertj to check whether the message in the reason is equal to a string
I am currently doing something similar:
assertThatThrownBy(() -> SUT.method()) .isExactlyInstanceOf(IllegalStateException.class) .hasRootCauseExactlyInstanceOf(Exception.class);
And I want to add an assertion to check the message in the root cause
Solution
Not exactly. At present, the best you can do is to use hasstacktracecontainer, for example
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");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码