【发布时间】:2013-05-11 06:24:31
【问题描述】:
JUnit's TemporaryFolder rule 的文档指出它创建的文件和文件夹是
"保证在测试方法结束时被删除(无论是 通过或失败)”
但是,断言 TemporaryFolder 不存在失败:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class MyTest {
@Rule
public TemporaryFolder _tempFolder = new TemporaryFolder();
@After
public void after() {
assertFalse(_tempFolder.getRoot().exists()); //this assertion fails!
}
@Test
public void pass() throws IOException {
assertTrue(true);
}
我还看到该文件确实存在于文件系统上。
为什么没有被删除?
【问题讨论】:
-
如果临时文件夹中的任何文件被锁定(例如未关闭OutputStream),临时文件夹将不会被删除。