my dog learned polymorphism*
The moose likes Testing and the fly likes how to handle the exception for JUnit code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "how to handle the exception for JUnit code" Watch "how to handle the exception for JUnit code" New topic
Author

how to handle the exception for JUnit code

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Suppose in my JUNIT code I have

public void testMethod() { // shall I just throw Exception
try {
String s = method();
assertTrue(s, null);
...
} catch(Exception e) {
// ??? what to do with Exception e ? // line 6
}
}


Suppose this junit method tests a method called "Method()" which returns a String value and this method throws "Exception" out. Then in my "testMethod()" code, I am wondering

a) if I just throw it out again, it does not sounds like a good way for testing

b) how about I catch it in "testMethod()" (line 6) ? If I catch it, shall I just ignore it ? Or shall I print out the information ? Or shall I write the error to a log file ?

What's the typical way to do it when we are using junit ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think that you'd better declare the exception in the throws clause of the test method.



[My Blog]
All roads lead to JavaRanch
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
a) if I just throw it out again, it does not sounds like a good way for testing.


Basically, your questioning is correct - you just have to decide on the basis of what your code needs to accomplish.

b) how about I catch it in "testMethod()" (line 6) ? If I catch it, shall I just ignore it ? Or shall I print out the information ? Or shall I write the error to a log file ?


Generally, write to a log file. Do not ignore exceptions without great thought as to what that will do. Occasionally that is the way do do it but maybe unchecked exception would be better for that harness.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Exception handling in JUnit tests is a bit different to in a typical application.

If an exception can happen as part of correct operation of the methods being tested, then you need to catch and handle the exception in your test method.



If the exception should never happen as part of correct operation, then your test method should throw it out to JUnit infrastructure. The JUnit infrastructure will interpret this as a test failure. There is no need to catch such exceptions in your test method and do fail(); in fact, this throws away useful information that JUnit could have used in its test report.



There is another situation. That is where you have a JUnit test whose purpose is to check that the method being tested does throw the exception it should throw, when given particular (erroneous) inputs. In that situation, you do something like this: -



Remember that it is at least as important for your JUnit tests to test unusual and exceptional cases as it is to test the normal case. Many people would say it is more important, because a typical "smoke test" will exercise the normal case, but only rigorous testing will exercise unusual cases.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
It depends...

Normally if I'm testing something that throws a checked exception I'll explicitly put a fail() in the catch block when I'm testing the happy path. When I'm testing the exception path, I'll put the fail in the try block after the line that should throw the exception and an explicit pass()/assertTrue(true) in the catch block.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to handle the exception for JUnit code