Class TemporaryFolder

  • All Implemented Interfaces:
    TestRule

    public class TemporaryFolder
    extends ExternalResource
    The TemporaryFolder Rule allows creation of files and folders that should be deleted when the test method finishes (whether it passes or fails). By default no exception will be thrown in case the deletion fails.

    Example of usage:

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

    TemporaryFolder rule supports assured deletion mode, which will fail the test in case deletion fails with AssertionError.

    Creating TemporaryFolder with assured deletion:

      @Rule
      public TemporaryFolder folder= TemporaryFolder.builder().assureDeletion().build();
     
    Since:
    4.7
    • Constructor Detail

      • TemporaryFolder

        public TemporaryFolder()
        Create a temporary folder which uses system default temporary-file directory to create temporary resources.
      • TemporaryFolder

        public TemporaryFolder​(File parentFolder)
        Create a temporary folder which uses the specified directory to create temporary resources.
        Parameters:
        parentFolder - folder where temporary resources will be created. If null then system default temporary-file directory is used.
    • Method Detail

      • newFile

        public File newFile​(String fileName)
                     throws IOException
        Returns a new fresh file with the given name under the temporary folder.
        Throws:
        IOException
      • newFile

        public File newFile()
                     throws IOException
        Returns a new fresh file with a random name under the temporary folder.
        Throws:
        IOException
      • newFolder

        public File newFolder​(String path)
                       throws IOException
        Returns a new fresh folder with the given path under the temporary folder.
        Throws:
        IOException
      • newFolder

        public File newFolder​(String... paths)
                       throws IOException
        Returns a new fresh folder with the given paths under the temporary folder. For example, if you pass in the strings "parent" and "child" then a directory named "parent" will be created under the temporary folder and a directory named "child" will be created under the newly-created "parent" directory.
        Throws:
        IOException
      • newFolder

        public File newFolder()
                       throws IOException
        Returns a new fresh folder with a random name under the temporary folder.
        Throws:
        IOException
      • getRoot

        public File getRoot()
        Returns:
        the location of this temporary folder.
      • delete

        public void delete()
        Delete all files and folders under the temporary folder. Usually not called directly, since it is automatically applied by the Rule.
        Throws:
        AssertionError - if unable to clean up resources and deletion of resources is assured.