[英]How do I unit-test saving file to the disk with FileUtils?
I know this variation has been asked before. 我知道以前已经有人问过这种变化。
But, One of my functions is using Common's FileUtils . 但是,我的功能之一是使用Common的FileUtils 。 Here, it only takes File object and String. 在这里,它仅使用File对象和String。 Is there any way to unit test this? 有什么办法可以对此进行单元测试?
Of course. 当然。 Use a temporary folder, save your file there, and delete the folder after the test. 使用临时文件夹,将文件保存在此处,然后在测试后删除该文件夹。
If you use JUnit, look at the TemporaryFolder
JUnit rule (it creates a temp folder for you and takes care of the deleting). 如果您使用JUnit,请查看TemporaryFolder
JUnit规则(它会为您创建一个临时文件夹并负责删除)。
Example code: 示例代码:
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.