我需要一个像这样的文件夹结构:
home
--level1
--level2a
--level2b
我可以使用junit的临时目录创建第一个级别,但是如何在其下创建级别?
@Rule
public TemporaryFolder home= new TemporaryFolder();
@Before
public void setUp()
{
File level1= home.newFolder("level1");
}
答案 0 :(得分:2)
试试这个:
package com.sandbox;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import static org.junit.Assert.assertTrue;
public class SandboxTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
private File level1;
private File level2a;
private File level2b;
@Before
public void setUp() throws Exception {
level1 = folder.newFolder("level1");
level2a = folder.newFolder("level1/level2a");
level2b = folder.newFolder("level1/level2b");
}
@Test
public void whenCreatingNestedFoldersThenAllGetDeleted() {
assertTrue(level1.exists());
assertTrue(level2a.exists());
assertTrue(level2b.exists());
System.out.println(level1.getAbsolutePath());
}
}
它将完全按照您的预期运作。测试运行后,所有这些都将被清理干净。
答案 1 :(得分:2)
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import static org.junit.Assert.assertTrue;
public class SandboxTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
private File level1;
private File level2a;
private File level2b;
@Before
public void setUp() throws Exception {
level1 = folder.newFolder("level1");
level2a = folder.newFolder("level1", "level2a");
level2b = folder.newFolder("level1", "level2b");
}
@Test
public void whenCreatingNestedFoldersThenAllGetDeleted() {
assertTrue(level1.exists());
assertTrue(level2a.exists());
assertTrue(level2b.exists());
System.out.println(level1.getAbsolutePath());
}
}