Class TemporaryFolder

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class TemporaryFolder
    extends org.junit.rules.ExternalResource
    Basically copied from TemporaryFolder which looks different in different junit versions and does not reset the private field folder on the after() event. Another change is that the folder is only created on demand. The TemporaryFolder Rule allows creation of files and folders that are guaranteed to be deleted when the test method finishes (whether it passes or fails):
     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");
                    // ...
            }
     }
     
    Since:
    2.7
    Noextend:
    This class is not intended to be subclassed by clients.
    • Constructor Summary

      Constructors 
      Constructor Description
      TemporaryFolder()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addSourceInfo​(java.io.File tempDir)
      generate '.createdBy' file with a stack trace so tests that leak temp folders can be identified later.
      protected void after()  
      protected void before()  
      void create()
      for testing purposes only.
      java.io.File createTempFile​(java.lang.String prefix, java.lang.String suffix)
      Same signature as File.createTempFile(String, String).
      void delete()
      Delete all files and folders under the temporary folder.
      java.io.File getRoot()  
      boolean isInitialized()  
      java.io.File newFile()
      Returns a new fresh file with a random name under the temporary folder.
      java.io.File newFile​(java.lang.String fileName)
      Returns a new fresh file with the given name under the temporary folder.
      java.io.File newFolder()
      Returns a new fresh folder with a random name under the temporary folder.
      java.io.File newFolder​(java.lang.String... folderNames)
      Returns a new fresh folder with the given name under the temporary folder.
      • Methods inherited from class org.junit.rules.ExternalResource

        apply
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TemporaryFolder

        public TemporaryFolder()
    • Method Detail

      • before

        protected void before()
                       throws java.lang.Throwable
        Overrides:
        before in class org.junit.rules.ExternalResource
        Throws:
        java.lang.Throwable
      • after

        protected void after()
        Overrides:
        after in class org.junit.rules.ExternalResource
      • isInitialized

        public boolean isInitialized()
      • create

        public void create()
                    throws java.io.IOException
        for testing purposes only. Do not use.
        Throws:
        java.io.IOException
      • newFile

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

        public java.io.File createTempFile​(java.lang.String prefix,
                                           java.lang.String suffix)
                                    throws java.io.IOException
        Same signature as File.createTempFile(String, String).
        Throws:
        java.io.IOException
      • newFile

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

        public java.io.File newFolder​(java.lang.String... folderNames)
        Returns a new fresh folder with the given name under the temporary folder.
      • newFolder

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

        protected void addSourceInfo​(java.io.File tempDir)
                              throws java.io.IOException
        generate '.createdBy' file with a stack trace so tests that leak temp folders can be identified later.
        Throws:
        java.io.IOException
      • getRoot

        public java.io.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