What is difference between tearDownClass() & tearDown() methods?
Where can I find documentation for both.
junit.org documentation of JUnit listed only tearDown() not tearDownClass(): http://www.junit.org/apidocs/junit/framework/TestCase.html#setUp()
Use the API's tearDownAfterClass() and tearDown() with the Annotations @AfterClass and @After. The code within tearDownAfterClass() would be executed only once after all the unit tests written in Junit have been executed. Clean up code can be written here to release the resources after all the tests have been executed. The code within tearDown() would be executed after executing each of the test scenarios.
These API's are part of JUnit 4.
Below is a sample code to understand the invocation of these API's:
public class TestJUnit {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("Executing a JUNIT test file");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("Execution of JUNIT test file done");
}
@Before
public void setUp() throws Exception {
System.out.println("Executing a new test");
}
@After
public void tearDown() throws Exception {
System.out.println("Execution done");
}
@Test
public void test1() {
System.out.println("test1 ...");
}
@Test
public void test2(){
System.out.println("test2 ...");
}
}
Output: Executing a JUNIT test file Executing a new test test1 Execution done Executing a new test test2 Execution done Execution of JUNIT test file done
The API's setUpBeforeClass() and setUp() with the Annotations @BeforeClass and @Before respectively would behave as follows:
setUpBeforeClass - Useful to have initialization code here. Code written in this method would get executed only once and execution would happen prior to execution of the individual tests.
setUp() - Code within this block would get executed prior to each of the individual tests.
There's a AfterClass annotation in the JUnit 4.x API, is that what you meant?
tearDown
occurs after executing each test method of the TestCase. There is a separate hook (the AfterClass I linked to) that executes after all the test methods of the TestCase have run.
I don't think the 3.x Junit API had any concept of an after-class teardown. Maybe you're thinking of TestNG?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With