You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by "Julian M. Savage" <js...@fisci.com> on 2000/11/10 05:01:05 UTC

[PATCH] JUnitTestRunner.java modification

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;


Re: [PATCH] JUnitTestRunner.java modification

Posted by "Julian M. Savage" <js...@fisci.com>.
Dirk,

I don't think I stated myself very clearly in my last message, but your
message has enabled me to focus my comments a bit. All our existing test
cases derive from a single class, and my patch makes it possible to only
change that class, plus ant, and not have to go through and make tedious
changes to 160 existing classes which derive from our base test class.

The way that the JUnitTestRunner and the test runners which are part of the
project are implemented makes it almost impossible to change which method is
invoked without overriding a static method.

The choices I could see were:
* modify all my classes to have only one method starting with the string
"text"
* modify all my classes to use JUnit directly. This would be a *lot* of
work.
* modify all my classes to have a static suite() method.
* modify one of my classes, and a few lines of code in the JUnitTestRunner.

I took the last option.

What I'd like to see is the ability to create a single object, and add a
list of methods I want the test suite to invoke. This would provide me with
an evolutionary migration path to using JUnit only. I can see the reasoning
behind doing things the way they are done in JUnit, but they class with our
existing code base. I submitted my patch in the hope that others would
either benefit from it, or point out an easier way for me to do things.

Julian.
----- Original Message -----
From: "Dirk Weigenand" <Di...@oracle.com>
To: <an...@jakarta.apache.org>
Sent: Friday, November 10, 2000 8:21 PM
Subject: Re: [PATCH] JUnitTestRunner.java modification


> Hello Julian,
>
> "Julian M. Savage" wrote:
> >
>
> >
> > 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.
> >
>
> If i understand you correctly you want to invoke only one method per
class.
> JUnit
> looks in the test class for public void methods whose name starts with
'test'.
> If you make sure that there is only one such method in your test classes
it
> will
> the only one to be called.
>
> regards,
> Dirk
> --
> Daddy, what does "format disk c: complete" mean?
> Who is General Failure and why is he reading my disk?


Re: [PATCH] JUnitTestRunner.java modification

Posted by Dirk Weigenand <Di...@oracle.com>.
Hello Julian,

"Julian M. Savage" wrote:
> 

> 
> 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.
> 

If i understand you correctly you want to invoke only one method per class.
JUnit
looks in the test class for public void methods whose name starts with 'test'.
If you make sure that there is only one such method in your test classes it
will
the only one to be called.

regards,
	Dirk
-- 
Daddy, what does "format disk c: complete" mean?
Who is General Failure and why is he reading my disk?