assertj: Assertions.assertThatThrownBy doesn't support the as/describedAs API
This:
Assertions.assertThatThrownBy(something::call)
.as("This is not OK")
.isInstanceOf(expectedException);
yields the default message:
java.lang.AssertionError: Expecting code to raise a throwable.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 20 (10 by maintainers)
I can agree with calling
hasBeenThrown()inAbstractThrowableAssert.We can’t rename
assertThatCodeas it would be a breaking change, I’m a bit reluctant to addassertThatCallas an aliasassertThatCodebecause users might wonder what is the difference withassertThatCode.Documented.
Yes, I (finally!) see the problem.
assertThatThrownByasserts not null on the throwable before any chained calls,asor otherwise. I suppose this was made so users can check whether the call through anything at all… I admit that I have indeed used this a couple of times.The only solution I see is that we use
assertThatCode. However, the message in case of throwable beingnullshould be fixed.The fix: All public methods in
AbstractThrowableAssertwhich check parts of the throwable (isSameAs,isInstanceOf,hasMessage, …) should first callhasBeenThrown().There is no point in checking a throwable which is
nullbecause this is de-facto usage of a part of theAbstractObjectAssertAPI not intended for throwables. Anullthrowable is already an error, not a regular value which should be checked.And I would prefer the method
assertThatCodebe calledassertThatCallsince that’s more specific and after all, its argument is called...Callable.