9

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?

Community
  • 1
  • 1
Cincinnati Joe
  • 2,077
  • 6
  • 23
  • 32
  • Assume we don't have time currently to manually convert and verify all the tests (and train all involved developers on using the junit 4 annotations), but just want to address the few classes that require an AfterClass equivalent in junit 3. – Cincinnati Joe Oct 30 '09 at 13:11

2 Answers2

20

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?

public static Test suite() {
    return new TestSetup(new TestSuite(YourTestClass.class)) {

        protected void setUp() throws Exception {
            System.out.println(" Global setUp ");
        }
        protected void tearDown() throws Exception {
            System.out.println(" Global tearDown ");
        }
    };
}
Cincinnati Joe
  • 2,077
  • 6
  • 23
  • 32
  • did you run this on Android? I have the exactly same code but I'm getting a ClassNotFound exception. Have you already faced this?does YourTestClass extends TestCase? – Felipe Mosso Jul 23 '14 at 14:29
  • I could execute my test with this code now but it seems the suite() method is not called. What did you do to make such call to this method? I though it would be called automatically – Felipe Mosso Jul 23 '14 at 19:51
  • @FelipeMosso if you tell JUnit (or your IDE) to run a single JUnit3-style test class and that class has a public static `suite()` method that returns a `Test` (or subclass of test) then the `suite()` method will be called to generate the suite. But if you manually create suites yourself, you need to ensure that the suite() method is used (possibly by using the `TestSuite` constructor that takes in a class) – NamshubWriter Mar 27 '15 at 02:45
2

In JUnit 4, your test class doesn't need to extend TestCase. Instead you must ensure that the name of your test class matches *Test and each test method is annotated with @Test. Have you tried making these changes, then adding the @AfterClass annotation to the relevant method?

There are annotations to replace most/all functionality you may currently be using from TestCase.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • In parallel to trying to get a class-wide teardown, I'm also trying to just convert one class to junit 4 annotations. Not having much luck yet as Eclipse/command-line then thinks I don't have any tests to be run. – Cincinnati Joe Oct 30 '09 at 13:12
  • I've updated my answer, the advice I gave previously was completely wrong and I deserve to have been downvoted to hell. Please follow the instructions above and try again. – Dónal Oct 30 '09 at 15:05