报告未在 finally 块内执行的 JUnit 3 的 super.tearDown() 方法的调用。 如果 tearDown() 方法中还有可能会在 super.tearDown() 调用之前抛出异常的其他方法调用,这可能会导致不一致和泄漏。

示例:


  public class AnotherTest extends CompanyTestCase {
    private Path path;

    @Override
    protected void setUp() throws Exception {
      super.setUp();
      path = Files.createTempFile("abcde", ".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("abcde", ".tmp");
    }

    @Override
    protected void tearDown() throws Exception {
      try {
        Files.delete(path);
      } finally {
        super.tearDown();
      }
    }
  }