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
assertThatCode
as it would be a breaking change, I’m a bit reluctant to addassertThatCall
as an aliasassertThatCode
because users might wonder what is the difference withassertThatCode
.Documented.
Yes, I (finally!) see the problem.
assertThatThrownBy
asserts not null on the throwable before any chained calls,as
or 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 beingnull
should be fixed.The fix: All public methods in
AbstractThrowableAssert
which check parts of the throwable (isSameAs
,isInstanceOf
,hasMessage
, …) should first callhasBeenThrown()
.There is no point in checking a throwable which is
null
because this is de-facto usage of a part of theAbstractObjectAssert
API not intended for throwables. Anull
throwable is already an error, not a regular value which should be checked.And I would prefer the method
assertThatCode
be calledassertThatCall
since that’s more specific and after all, its argument is called...Callable
.