Wednesday, 14 August 2013

Should jUnit test cases handle default exceptions in a throws declaration or in a try catch block

Should jUnit test cases handle default exceptions in a throws declaration
or in a try catch block

If I write test cases for a function that throws a bunch of exceptions
should I add a throws declaration for these exceptions in my test method
or should I catch each individual exception. What is the correct way of
going about it? I believe try-catch is a better way but in the catch block
should I print the stacktrace?
For example, I have a method getGroups(String name) that throws
AuthenticationException. If I write a test case to check if an
IllegalArgumentException is being thrown when the name parameter is null,
how do I handle the AuthenticationException? Do I add it to throws part of
my method or should I enclose the exception in a try-catch block.
@Test
public void testGetGroupsWithNull() throws AuthenticationException {
thrown.expect(IllegalArgumentException.class);
getGroups(null);
}
In the above test case I just added a throws AuthenticationException, but
I would like to know if it is better to enclose the exception in a
try-catch block and what shoudld I do after catching the exception. I
could print the stack trace.

No comments:

Post a Comment