Verifying that exception was thrown

12,138 views
Skip to first unread message

Domas

unread,
Jan 13, 2009, 5:35:41 AM1/13/09
to mockito
Is there a way to verify that an exception was thrown while invoking
some method?
Or should I use "expectedExceptions" argument of
org.testng.annotations.Test annotation?
I didn't want to split all exception testing into separate methods.

Domas

szczepiq

unread,
Jan 13, 2009, 7:26:38 AM1/13/09
to moc...@googlegroups.com
Hi,

Usually I do something like that:

when(mock.foo()).thenThrow(exc);

try {
system.doStuff();
fail();
} catch(SomeSpecificException e) {
//you can assert the exception here - if it makes sense
}

Cheers,
Szczepan Faber

Domas

unread,
Jan 14, 2009, 3:46:47 AM1/14/09
to mockito
Hi,
> Usually I do something like that:
>
> when(mock.foo()).thenThrow(exc);
>
> try {
>   system.doStuff();
>   fail();} catch(SomeSpecificException e) {
>
>   //you can assert the exception here - if it makes sense
>
> }
Thanks for suggestion.
I just was expecting something like:
Foo foo = spy(this.foo);
verifyThrows(foo.test(), RuntimeException.class);

Anyway, I guess this is responsibility of testing framework, not
Mockito.

Domas
Reply all
Reply to author
Forward
0 new messages