在JUnit中删除文件和目录

Fra*_*ith 4 java junit file delete-file

我正在为在目录中创建文件的方法编写测试.这是我的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

  }
Run Code Online (Sandbox Code Playgroud)

我能够使用deleteOnExit()删除文件,但我无法使用delete或deleteOnExit删除该目录.我还将在此测试脚本中对其他方案执行其他测试,因此我不想使用deleteOnExit.

我不知道为什么我不能在JUnit测试脚本中删除它,而我可以在运行时删除由FileWriter创建和修改的文件,而代码不是JUnit测试.我还尝试在测试方法之后执行infiniteLoop并手动删除文件,但它告诉我其他程序仍在使用该文件,尽管我可以修改其内容.

希望有人可以建议删除测试期间创建的文件和目录的方法.感谢:D

为了更清楚,我测试的方法看起来像调用FileWriter的Unit测试方法

编辑:这是测试的方法

    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();
           }

    }
Run Code Online (Sandbox Code Playgroud)

我不能像其他开发人员创建它那样修改此方法.我刚刚被指示为测试自动化创建单元测试.谢谢.

Col*_*n D 8

使用@Rule注释和TemporaryFolder您需要删除的文件夹的类.

http://kentbeck.github.com/junit/javadoc/4.10/org/junit/Rule.html(未找到404)

通过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");
    // ...
  }

}
Run Code Online (Sandbox Code Playgroud)

  • 您可以在 TemporaryFolder 内创建要创建文件夹的文件夹吗?即:mockDirPath 是 TemporaryFolder 的子文件夹。 (2认同)

dav*_*ago 5

这通常是我清理文件的方式:

@AfterClass
public static void clean() {
    File dir = new File(DIR_PATH);
    for (File file:dir.listFiles()) {
        file.delete();
    }
    dir.delete();
}
Run Code Online (Sandbox Code Playgroud)

您的目录必须为空才能删除它,请确保没有其他测试方法在其中创建更多文件。

确保在finally块中关闭FileWriter实例