我想创建一个代表这种树的baseFolder的JUnit TemporyFolder:
baseFolder/subFolderA/subSubFolder
/subFolderB/file1.txt
据我所知,我可以设置一个临时文件夹,然后可以使用"newFolder()"创建位于该文件夹中的伪文件夹。但是我怎么在下面创建图层呢?特别是在测试后清理的方式。
temporaryFolder.newFolder(String... folderNames)
以整个层次结构作为参数:
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void test() throws Exception {
File child = temporaryFolder.newFolder("grandparent", "parent", "child"); //...
assertEquals("child", child.getName());
assertEquals("parent", child.getParentFile().getName());
assertEquals("grandparent", child.getParentFile().getParentFile().getName());
System.out.println(child.getAbsolutePath());
}
它通过测试并打印:
/var/folders/.../T/junit8666449860303204067/grandparent/parent/child
TemporaryFolder有一个方法newFolder(String...folderNames)
,允许您创建子目录。
tempFolder.newFolder("subFolderA", "subSubFolder")
http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html newFolder(以…)