在JUnit中删除文件和目录 [英] Deleting File and Directory in JUnit

查看:290
本文介绍了在JUnit中删除文件和目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在目录中创建文件的方法编写测试。这是我的JUnit测试的样子:

  @Before 
public void setUp(){
objectUnderTest =新的ClassUnderTest();
//使用powermock分配另一个测试目录路径
WhiteBox.setInternalState(objectUnderTest, dirPathField,mockDirPathObject);

nameOfFile = name.txt;
textToWrite =某些文本;
}

@ Test
public void shouldCreateAFile(){


//创建文件并编写测试
objectUnderTest。 createFile(nameOfFile,textToWrite);
/ *此方法在模拟路径对象中创建文件并执行
FileWriter.write(text);
FileWriter.close();
* /

文件期望=新文件(mockPathObject + \\ + nameOfFile);
assertTrue(expect.exist());

//声明文本是否在文件中->如果第一个断言失败
}

@After
public void tearDown(){
File destroyFile = new File(mockPathObject + \\ \ + nameOfFile);
File destroyDir = new File(mockPathObject);

//这是我的问题
destroyFile.delete(); //为什么返回假?
destroyDir.delete(); //由于文件未在

}以上被删除,因此也会返回false。

我可以使用deleteOnExit()删除文件,但无法使用delete或deleteOnExit删除目录。我还将在此测试脚本中针对其他场景执行其他测试,所以我不想使用deleteOnExit。



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



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



为了更加清楚,我测试的方法如下:调用FileWriter的单元测试方法



编辑:以下是测试方法

  public void createFile(String fileName,String text){

// SOME_PATH是一个静态字符串,它是类$的字段b $ b文件目录=新文件(SOME_PATH); //我使用白盒修改了SOME_PATH以测试

if(!dir.exists()){
booelan createDir = dir.mkdirs();
if(!createDir){
sysout( can not make dir);
的回报;
}
}


try {
FileWriter fileWrite = new FileWriter(dir.getAbsolutePath()+ / + fileName,true);
fileWrite.write(text);
fileWrite.close();
}
catch(Exception e){
e.printStackTrace();
}

}

我无法将此方法修改为其他方法开发人员创建了它。我刚刚被指示要创建用于自动化测试的单元测试。谢谢。

解决方案

使用@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

 公共静态类HasTempFolder {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
公共无效testUsingTempFolder()引发IOException {
File createdFile = folder.newFile( myfile.txt);
文件createdFolder = folder.newFolder( subfolder);
// ...
}

}


I'm writing a test for a method that creates a file in a directory. Here's what my JUnit test looks like:

  @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.

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.

Hope somebody can suggest a way to delete the files and directories created during the tests. Thanks :D

For more clarity, the method I test looks like this Unit testing method that invokes 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.

解决方案

Use the @Rule annotation and the TemporaryFolder classfor the folder that you need to delete.

http://kentbeck.github.com/junit/javadoc/4.10/org/junit/Rule.html (404 not found)

Update example of usage by 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");
    // ...
  }

}

这篇关于在JUnit中删除文件和目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆