finally
块内执行的 JUnit 3 的 super.tearDown()
方法的调用。
如果在调用 super.tearDown()
之前抛出异常,可能会导致不一致和泄露。
示例:
public class AnotherTest extends CompanyTestCase {
private Path path;
@Override
protected void setUp() throws Exception {
super.setUp();
path = Files.createTempFile("File", ".tmp");
}
@Override
protected void tearDown() throws Exception {
Files.delete(path);
super.tearDown();
}
}
改进后的代码:
public class AnotherTest extends CompanyTestCase {
private Path path;
@Override
protected void setUp() throws Exception {
super.setUp();
path = Files.createTempFile("File", ".tmp");
}
@Override
protected void tearDown() throws Exception {
try {
Files.delete(path);
} finally {
super.tearDown();
}
}
}