You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Paul Michali <pc...@cisco.com> on 2001/09/20 23:19:35 UTC

Re: Using TestSetup in JUnit with Ant (was: How to set a property from a custom task?)

> > Timothy Shadel wrote:
> > [The following link is on info about using TestSetup to perform one time
> >  action for all tests]
> > http://junit.sourceforge.net/doc/faq/faq.htm#How%20do%20I%20run%20setup%20code%20only%20once


> I Wrote:
> Will that work, given that I'm running JUnit from Ant and letting JUnit
> find all my test cases via Reflection? If not, is there a way to do this?


Timothy Shadel then wrote:

> [info on building suites from test cases and suites from suites...]
> ...
> 
> I'm pretty sure you could wrap your test case classes using the TestSetup class described in the first URL, and where it said "...add your tests and suites here... ", you could use the methods above (e.g. TestSuite suite= new TestSuite(MoneyTest.class);) to allow Java Reflection to figure out which tests to run.

I thought the way JUnit worked was that it used to reflection to find
all Test classes
and test methods and then invoked the test methods (ones that start with
test...). In
that case, if I override TestCase with TestSetup, will JUnit use that or
will it just
grab and run the tests (thus not running the TestSetup.setup() method)?

Has anyone done this? I'd like to find out before I make the decision to
alter my
test suite (as I trying to figure out how to either do this with a
custom Task in Ant
or through the JUnit suite itself).


PCM (Paul Michali)

Carrier Voice Gateway Business Unit (CVGBU)
Cisco Systems, Inc.
250 Apollo Drive
Chelmsford, MA 01824

Phone : (800) 572-6771 x 45817  (978) 244-5817 [direct]
Paging: (800) 365-4578 [voice]  pcm@epage.cisco.com [email page]

Re: Using TestSetup in JUnit with Ant (was: How to set a property from a custom task?)

Posted by David Walend <df...@cornell.edu>.
Paul Michali wrote:

>
>I thought the way JUnit worked was that it used to reflection to find
>all Test classes
>and test methods and then invoked the test methods (ones that start with
>test...). 
>
Yes.

>In
>that case, if I override TestCase with TestSetup, will JUnit use that or
>will it just
>grab and run the tests (thus not running the TestSetup.setup() method)?
>
To run my set up and tear down once for a whole block of tests, I use it 
like this:

--------------
public class JDBCTests extends TestCase
{
    public JDBCTests(String testName)
    {
        super(testName);
    }


    public static Test suite()
    {
        TestSuite suite=new TestSuite();

        suite.addTest(NestedSQLExceptionTest.suite());
        suite.addTest(ResultSetWrongSizeExceptionTest.suite());
        suite.addTest(ConnectionSourceTest.suite());
        suite.addTest(DatabaseUtilsTest.suite());
        suite.addTest(SQLStatementStringTest.suite());

        return new JDBCTestSetup(suite);
    }
}

-----------------

You'll probably want use it like this, to save some typing:

------------------

public class YourTests extends TestCase
{
//all your tests

    public static Test suite()
    {
        TestSuite suite=new TestSuite(YourTests.class);

        return new YourTestSetup(suite);
    }
}

-----------------

The TestSuite constructor with a Class argument uses reflection. If you 
give it a TestSetup subclass, you'll most likely get a 
NoSuchMethodException when it looks for constructors.

If you want each test to run its own set up and tear down, you'll want 
to do this:

-----------------

    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JDBCTestSetup(new 
DatabaseUtilsTest("testExecuteUpdate")));
        suite.addTest(new JDBCTestSetup(new 
DatabaseUtilsTest("testExecuteUpdateWithInt")));
        suite.addTest(new JDBCTestSetup(new 
DatabaseUtilsTest("testExecuteQuery")));

        return suite;
    }
----------------

But you're just starting a mail server, if it isn't there, right?

(Here, Dave shamelessly plugs his own patch.) If you want a cleaner 
stop, try using this patch: 
https://sourceforge.net/tracker/?func=detail&aid=436182&group_id=15278&atid=315278 
. If the test itself throws an exception, it still calls tearDown(), to 
clean up the mess.

>
>Has anyone done this? I'd like to find out before I make the decision to
>alter my
>test suite (as I trying to figure out how to either do this with a
>custom Task in Ant
>or through the JUnit suite itself).
>
Works great.

Hope that helps,

Dave