Anybody using JUnit's ExceptionTestCase ?

To write a unit test that expects an exception of a particular type to be thrown is possible by using JUnit's class junit.framework.ExceptionTestCase. However the usage of this class (that inherits from junti.framework.TestCase) is ugly (details such as import statements omitted):

1. Test class
public void MyTestCase extends ExceptionTestCase {
public void testXYZ throws Exception {
MyClass myObj = new MyClass();
myobj.doXYZ(); //throws MyException

2. Test suite
public void MyTestSuite extends TestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("description");
suite.addTest(new MyTest("testXYZ", MyException.class));
return suite;

I've never ever seen such a stupid thing. Maybe that is the reason for ExceptionTestCase currently not being existent in JUnit 4's CVS version.

Also see the JUnit book for further information.

No comments: