I'm really a newbie to JUnit and unit testing in general and I'm struggling to find the right approach. What is the better way to deal with unexpected exceptions, and why?
Method A:
Method B:
throws Exception and let anything unexpected "bubble up" completely out of the testAnd to add to the confusion, by saying "unexpected exception", I mean either one of these things:
I'm aware this question comes out a bit confusing, I'm getting lost in it myself, but hopefully someone will give me any kind of hint. Won't blame you for downvotes, it's still worth the risk :)
I got the feeling, none of the answers so far really got to the point of the question. The OP explicitly asks for the handling of unexpected exceptions. My two cents on this topic are:
It depends on the level of verbosity you want to achieve:
Usually, you should strive for short tests which pinpoint one aspect of the code. Ideally, only a handful of methods need to be called and only one or two of them may raise an unexpected exception. In this case, adding a throws-clause for all checked exceptions should be sufficient to analyze the problem when you test fails. I prefer this solution because it's easier to write, shorter and more comprehensive.
Example:
@Test
public void testPrivateMethod() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//...
Method method = MyClass.class.getMethod("privateMethod");
method.setAccessible(true);
boolean result = method.invoke(myInstance);
assertTrue(result);
}
If the test code needs to be more complicated multiple methods may be responsible for raising an exception. Probably, some of them even throw exceptions of the same kind. In this case, try-catch blocks may be beneficial for locating the problem when your test case fails. However, this produces more code and may render the tests less readable.
Example:
@Test
public void testPrivateMethod() {
//...
Method method;
try {
Method method = MyClass.class.getMethod("privateMethod");
method.setAccessible(true);
boolean result = method.invoke(myInstance);
assertTrue(result);
} catch (NoSuchMethodException | SecurityException e) {
fail("Could not access method 'privateMethod'.");
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
fail("Call to 'privateMethod' raised an exception.")
}
}
I hope I got the intention of the question right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With