Deleting File and Directory in JUnit

前端 未结 4 1267
执笔经年
执笔经年 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条回答
  •  忘了有多久
    2021-02-09 08:21

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

提交回复
热议问题