If you need something similar to the workaround mentioned for @BeforeClass , you can keep track of how many tests have been run, and then once all the tests have been run, finally execute your final cleanup code.
public class MyTestClass { // ... private static int totalTests; private int testsRan; // ... @BeforeClass public static void beforeClass() { totalTests = 0; Method[] methods = MyTestClass.class.getMethods(); for (Method method : methods) { if (method.getAnnotation(Test.class) != null) { totalTests++; } } } // test cases... @After public void after() { testsRan++; if (testsRan == totalTests) { // One time clean up code here... } } }
It is assumed that you are using JUnit 4. If you need to consider methods inherited from the superclass, see this , as this solution does not get the inherited methods.
kingkupps
source share