java 在 JUnit 中删除文件和目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11884141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting File and Directory in JUnit
提问by Frank Smith
I'm writing a test for a method that creates a file in a directory. Here's what my JUnit test looks like:
我正在为在目录中创建文件的方法编写测试。这是我的 JUnit 测试的样子:
@Before
public void setUp(){
objectUnderTest = new ClassUnderTest();
//assign another directory path for testing using powermock
WhiteBox.setInternalState(objectUnderTest, "dirPathField", mockDirPathObject);
nameOfFile = "name.txt";
textToWrite = "some text";
}
@Test
public void shouldCreateAFile(){
//create file and write test
objectUnderTest.createFile(nameOfFile, textToWrite);
/* this method creates the file in mockPathObject and performs
FileWriter.write(text);
FileWriter.close();
*/
File expect = new File(mockPathObject + "\" + nameOfFile);
assertTrue(expect.exist());
//assert if text is in the file -> it will not be called if first assert fails
}
@After
public void tearDown(){
File destroyFile = new File(mockPathObject + "\" + nameOfFile);
File destroyDir = new File(mockPathObject);
//here's my problem
destroyFile.delete(); //why is this returning false?
destroyDir.delete(); //will also return false since file was not deleted above
}
I was able to delete the File using deleteOnExit() but I will not be able to delete the directory using delete or deleteOnExit. I will also perform other test for other scenarios in this test script so I don't want to use deleteOnExit.
我可以使用 deleteOnExit() 删除文件,但无法使用 delete 或 deleteOnExit 删除目录。我还将在此测试脚本中针对其他场景执行其他测试,因此我不想使用 deleteOnExit。
I don't know why I cannot delete it in JUnit test script while I can delete a file created and modified by FileWriter in runtime when the code is not a JUnit test. I also tried performing an infiniteLoop after the test method and delete the file manually but it tells me that other program is still using the file though I'm able to modify its content.
我不知道为什么我不能在 JUnit 测试脚本中删除它,而当代码不是 JUnit 测试时,我可以删除运行时由 FileWriter 创建和修改的文件。我还尝试在测试方法之后执行无限循环并手动删除文件,但它告诉我其他程序仍在使用该文件,尽管我可以修改其内容。
Hope somebody can suggest a way to delete the files and directories created during the tests. Thanks :D
希望有人可以提出一种删除测试期间创建的文件和目录的方法。感谢:D
For more clarity, the method I test looks like this Unit testing method that invokes FileWriter
为了更清楚,我测试的方法看起来像这个调用 FileWriter 的单元测试方法
Edit:Here is the method to test
编辑:这是测试的方法
public void createFile(String fileName, String text){
//SOME_PATH is a static string which is a field of the class
File dir = new File(SOME_PATH); //I modified SOME_PATH using whitebox for testing
if(!dir.exists()){
booelan createDir = dir.mkdirs();
if(!createDir){
sysout("cannot make dir");
return;
}
}
try{
FileWriter fileWrite = new FileWriter(dir.getAbsolutePath() + "/" + fileName, true);
fileWrite.write(text);
fileWrite.close();
}
catch(Exception e){
e.printStackTrace();
}
}
I cannot modify this method as other developers created it. I was just instructed to create unit tests for test automation. Thanks.
我无法修改此方法,因为其他开发人员创建了它。我刚刚被指示为测试自动化创建单元测试。谢谢。
回答by Colin D
Use the @Rule annotation and the TemporaryFolder
classfor the folder that you need to delete.
使用@Rule 注释和TemporaryFolder
需要删除的文件夹的类。
http://kentbeck.github.com/junit/javadoc/4.10/org/junit/Rule.html(404 not found)
http://kentbeck.github.com/junit/javadoc/4.10/org/junit/Rule.html(404未找到)
Update example of usage by http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html:
通过http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html更新使用示例:
public static class HasTempFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
File createdFile= folder.newFile("myfile.txt");
File createdFolder= folder.newFolder("subfolder");
// ...
}
}
回答by davidmontoyago
This is how I usually clean up files:
这是我通常清理文件的方式:
@AfterClass
public static void clean() {
File dir = new File(DIR_PATH);
for (File file:dir.listFiles()) {
file.delete();
}
dir.delete();
}
Your directory must be empty in order to delete it, make sure no other test methods are creating more files there.
您的目录必须为空才能删除它,确保没有其他测试方法在那里创建更多文件。
Ensure you are closing the FileWriter instance in a finally block.
确保您在 finally 块中关闭 FileWriter 实例。
回答by adarshr
Ensure that the method objectUnderTest.createFile(nameOfFile, textToWrite)
actually closes any opened streams?
确保该方法objectUnderTest.createFile(nameOfFile, textToWrite)
实际上关闭了任何打开的流?
回答by Lho Ben
I think the best approche is te delete after JVM exit :
我认为最好的方法是在 JVM 退出后删除:
Path tmp = Files.createTempDirectory(null);
tmp.toFile().deleteOnExit();