Menu

#7 Assertion for expected exception

open
nobody
Main module (8)
5
2003-03-30
2003-03-23
No

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, ...});

Discussion

  • Vladimir Ritz Bossicard

    • labels: --> Main module
     
  • Jason Yip

    Jason Yip - 2003-04-02

    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.

     
  • Vladimir Ritz Bossicard

    Logged In: YES
    user_id=135259

    ThrowableAssert can help you check an exception and its message.

     
  • Jason Yip

    Jason Yip - 2003-04-15

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.