We have a lot of integration tests written using JUnit 3, though we’re now running them with 4.4. A few of these have a need for a tearDown method that runs after all the tests in the class are complete (to free some common resources).
I see that this can be done in junit 4 with @AfterClass (org.junit). However, mixing this into existing junit 3 tests that extend TestCase (junit.framework.*) doesn’t seem to work. [BTW is there a migration tool yet? Question 264680 indicated there wasn’t one a year ago.]
I’ve seen mention of using junit.extensions.TestSetup for this kind of thing. My brief testing of this didn’t seem work. Any examples?
Found the answer to my own question 🙂 I had tried this briefly before posting my question, but it wasn’t working for me. But I realize now that it’s because our test framework is invoking junit differently than the default, so it wasn’t calling the “suite” method that is needed in the following solution. In Eclipse, if I use the bundled junit runner to directly execute one Test/TestCase, it runs fine.
A way to do this setup/teardown once per class in junit 3 is to use TestSuite. Here’s an example on junit.org:
Is there a way to make setUp() run only once?