Wednesday, 18 September 2013

Way to short-circut propigation of checked exception in Junit

Way to short-circut propigation of checked exception in Junit

I just thought of something minor I would like to do and was curious if it
the ability to do it existed. It's purely a convenience syntactic-sugar
sort of thing, hardly mandatory to have, but a slight convenience I
thought may have been implemented at some point.
I have a method nestled 2-3 layers down in helper methods of a Junit test
case which throws a checked exception. I know for a fact that I want to
fail if this exception is ever thrown. However, if I just call Fail() I
lose the stack trace of the exception and the other conveniences that come
with allowing the exception to propagate
I know the 'standard' approach is to have the method throw the exception.
However, as I said, I'm buried a few methods deep already, and I suspect
this method will be called by many future helper methods. If I throw an
exception in this method I will have to constantly adding a throw clause
to all the methods that use this one method, directly or indirectly, which
could be quite a few methods. This is obviously doable, just a little
annoying; and, in theory at least, I could mask a situation where the
exception is thrown and I actually wanted to catch it if all my methods
already throw it.
Is there a way I can tell Junit to act as if the exception had propagated
up to the top level and fail, without having to have all my methods throw
it? So Junit reports a failure due to exception, with the stack trace and
all, immediately. Essentially I want a fail method that takes an
exception.
Does anything like this exist, or do I have to simply throw it?
ps. I know I could wrap it in a runtime exception and re-throw it, but I
don't really like that option either.

No comments:

Post a Comment