Sometimes I want test cases to inherit from other TestCase classes. The 2.1 version does not allow for this. However, adding this functionality is simple and straightforward.
In public TestSuite::TestSuite(final Class theClass) change the following code:
Class aClass= theClass; while ( Test.class.isAssignableFrom(aClass) ) { Method[] methods= aClass.getDeclaredMethods(); for (int i= 0; i < methods.length; i++) { Method m= methods[i]; String name= m.getName(); if (isTestMethod(m) && !hasTestMethod(m)) { Object[] args= new Object[1]; args[0]= name; try { Test t= (Test)constructor.newInstance(args); addTest(t); } catch (Throwable e) { addTest(warning("Cannot instantiate test case: "+name)); } } } aClass = aClass.getSuperclass(); }
Also, add the following method to TestSuite:
private boolean hasTestMethod(Method m) { for (Enumeration i= fTests.elements(); i.hasMoreElements(); ) { TestCase tc= (TestCase) i.nextElement(); String name= tc.name(); if (name.equals(m.getName())) return true; } return false; }