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