[英]How do I unit-test saving file to the disk with FileUtils?
当然。 使用临时文件夹,将文件保存在此处,然后在测试后删除该文件夹。
如果您使用JUnit,请查看TemporaryFolder
JUnit规则(它会为您创建一个临时文件夹并负责删除)。
示例代码:
public class YourTest {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
String filePath = folder.newFile("myfile.txt").getAbsolutePath();
FileUtils.writeFile(filePath, "some String");
assertTrue(new File(filePath).exists());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.