Assertion for expected exception
Brought to you by:
vbossica
What I end up doing in java/junit is this:
try
{
myObject.foo(...);
fail("message");
}
catch(FooBarException e)
{
// we want this...
}
In jython I would
assertRaises(FooBarException, myObject.foo, arg1, arg2,
...)
Now what would be nice to add to ThrowableAssert would be:
public assertRaises(String message, Class clazz, Object
instance, String methodName, Object[] args);
where I would call it like this:
assertRaises("message", FooBarException.class,
myObject, "foo", new Object[]{arg1, arg2, ...});
Logged In: YES
user_id=20134
Does that include checking the Exception message as well?
I've recently encountered cases where this was an issue.
The right type of Exception was caught but the wrong
message showed up. This tends to happen more if the
particular application uses nested or generic exceptions
versus exception hierarchies.
Logged In: YES
user_id=135259
ThrowableAssert can help you check an exception and its message.
Logged In: YES
user_id=20134
Took a closer look at ThrowableAssert... do you see
assertRaises returning true if FooBarException was nested
within a wrapping exception? I've recently encountered a
situation where we get a generic runtime exception wrapping
the expected exception when we're testing in 3 - tier mode vs
2 - tier mode. I don't want to have to write two sets of tests
so I've been thinking of writing an exception testing utility to
deal with this. All the better if this would be included in
ThrowableAssert. If I find some time, I'll do this and send a
patch.