Deleting File and Directory in JUnit

前端 未结 4 1273
执笔经年
执笔经年 2021-02-09 08:20

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(){
      objectUnderTes         


        
4条回答
  •  Happy的楠姐
    2021-02-09 08:34

    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.

提交回复
热议问题