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."
Joined: Oct 30, 2001
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.<br /> <br />#:^P
Joined: Dec 14, 2004
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.