a junit question: how to catch an Exception from a test case?
Hi,
I have a file like:
public class MyTest extends TestCase
{
@Override
public void setUp()
{
//code
}
public void testMethodA() throws Exception
{
runMethodA(); //this method has a clause "throws Exception"
super.assertEqual(bench_mark_file, generated_file);
}
// constructor
public MyTest(final String testName)
{
super(testName);
}
// build Test Suite
public static Test suite()
{
final TestSuite suite = new TestSuite("MyTest");
suite.addTest(new MyTest("testMethodA"));
return suite;
}
}
When running the junit test case "testMethodA", if runMethodA() throws
an Exception, the Exception object is never caught so that even the test
fails, the information shows up is very un-clear. I could re-write like
this:
public void testMethodA()
{
try
{
runMethodA(); //this method has a clause "throws Exception"
}
catch(Exception e)
{
super.fail(e.getMessage());
}
super.assertEqual(bench_mark_file, generated_file);
}
I prefer not this way, because it has more code to write. In my real
code, I may need to use several try/catch blocks, not just one. Anyway,
what is the point to have a junit test case throwing an Exception?
public void testMethodA() throws Exception // what is the point to have
"throws Exception" ?
Thank you very much.