Hello,
I've modified the JUnitTestRunner to look for a second suite() method on top
of the one it looks for already. This is so that you can implement a static
suite method which receives a Class object that describes the class that the
static suite method is being invoked for. I did this to avoid having to
implement the suite() method in each of my TestCase derived objects just so
that I could return a suite which constists of a single test.
JUnit already provides you with the chance to implement a suite() method
something like this:
public class MyTestCase extends TestCase {
public static TestSuite suite() {
return new TestSuite(new MyTestCaseDerivedClass("test"))
}
public void test() {
// Execute tests...
}
public void runTest() {
test();
}
}
What my change allows me to do is this:
public class BaseTestClass extends TestCase {
public static TestSuite suite(Class theClass) {
TestSuite suite = new TestSuite();
TestCase testCase = null;
try {
testCase = (TestCase) theClass.newInstance();
}
catch (IllegalAccessException e) {
System.out.println("Illegal Access Exception: Couldn't create
instance of" + theClass.getName());
}
catch (InstantiationException e) {
System.out.println("Instantiation Exception: Couldn't create
instance of " + theClass.getName());
}
suite.addTest(testCase);
return suite;
}
public void test() {
// Execute tests...
}
public void runTest() {
test();
}
}
With this in place, I can derive from BaseTestCase, and implement test(),
and be sure that this is the only method which will be called. I need
because of the way in which our tests were implemented before we had heard
of JUnit.
At least, I think I need it. If anyone can point me to a way to implement
the above without requiring the patch given below, please let me know.
Thanks,
Julian.
Index:
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/
junit/JUnitTestRunner.java,v
retrieving revision 1.3
diff -u -r1.3 JUnitTestRunner.java
---
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
2000/10/05 09:12:07 1.3
+++
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
2000/11/10 03:14:04
@@ -165,6 +165,18 @@
Method suiteMethod= testClass.getMethod("suite", new
Class[0]);
suite = (Test)suiteMethod.invoke(null, new Class[0]);
} catch(NoSuchMethodException e) {
+ try {
+ Method suiteMethod= testClass.getMethod("suite", new
Class[]{ Class.class });
+ suite = (Test)suiteMethod.invoke(null, new Class[]
{ testClass});
+ }
+ // Ignore exceptions
+ // If it doesn't work, we'll try something else.
+ catch (NoSuchMethodException f) {
+ }
+ catch(InvocationTargetException f) {
+ }
+ catch(IllegalAccessException f) {
+ }
} catch(InvocationTargetException e) {
} catch(IllegalAccessException e) {
}
@@ -174,7 +186,6 @@
// this will generate warnings if the class is no suitable
Test
suite= new TestSuite(testClass);
}
-
} catch(Exception e) {
retCode = ERRORS;
exception = e;