How can I make sure my JUnit test method's specific cleanup always runs?
I have a JUnit test class that has two test methods:
@Test
public void test1() {
// Setup: Create foo1.m
// Exercise
// Tear Down: Delete foo1.m
}
@Test
public void test2() {
// Setup: Create foo2.m
// Exercise
// Tear Down: Delete foo2.m
}
For each method, I would like to make sure that, if the Exercise section
fails for any reason, the Tear Down will still run. Note that the Setup
and Tear Down code for both test methods are different, so I don't think I
can use JUnit's @Before and @After annotations to do what I want.
I could put TRY-CATCH blocks into each test method:
@Test
public void test2() {
// Setup: Create foo2.m
try {
// Exercise
} finally {
// Tear Down: Delete foo2.m
}
}
but that seems ugly. Is there a way to make sure the test-method-specific
tear down code in each test method is executed, without using a TRY-CATCH
block?
No comments:
Post a Comment