Why not just create a class to execute the the code to do the setup, another class to do the tear down and use those classes to encapsulate the logic required to do the set up and the tear down respectively. You could then call this logic from where ever and when ever you needed it.
Something like this:
Business class:
package com.foo.bar.businesslogic.woozinator;
public class Woozinator {
public void doSomething() {
System.out.println("Doing something.");
System.out.println("Done.");
}
}
Test class:
package com.foo.bar.businesslogic.woozinator;
import org.junit.Test;
import com.foo.bar.businesslogic.testutil.WidgitUserTestSetupUtil;
import com.foo.bar.businesslogic.testutil.WidgitUserTestTearDownUtil;
public class WoozinatorTest {
@Test
public void shouldPassTest() {
WidgitUserTestSetupUtil.setUp();
// do test stuff here
WidgitUserTestTearDownUtil.tearDown();
}
}
Setup class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestSetupUtil {
public static void setUp() {
System.out.println("Setting up widget environment for widget user test");
// do stuff here
System.out.println("Done setting up environment");
}
}
Tear down class:
package com.foo.bar.businesslogic.testutil;
public class WidgitUserTestTearDownUtil {
public static void tearDown() {
System.out.println("Doing woozinator tear down.");
// undo stuff here
System.out.println("Done with woozinator tear down.");
}
}