VOOZH about

URL: https://junit.org/junit4/javadoc/latest/org/junit/rules/TemporaryFolder.html

⇱ TemporaryFolder (JUnit API)





Class TemporaryFolder

java.lang.Object
 👁 extended by 
org.junit.rules.ExternalResource 👁 extended by
org.junit.rules.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

TemporaryFolder.Builder
          Builds an instance of TemporaryFolder.
 
TemporaryFolder()
          Create a temporary folder which uses system default temporary-file directory to create temporary resources.
TemporaryFolder(File parentFolder)
          Create a temporary folder which uses the specified directory to create temporary resources.
TemporaryFolder(TemporaryFolder.Builder builder)
          Create a TemporaryFolder initialized with values from a builder.
 
after()
          Override to tear down your specific external resource.
before()
          Override to set up your specific external resource.
builder()
          Returns a new builder for building an instance of TemporaryFolder.
create()
          for testing purposes only.
delete()
          Delete all files and folders under the temporary folder.
getRoot()
           
newFile()
          Returns a new fresh file with a random name under the temporary folder.
newFile(String fileName)
          Returns a new fresh file with the given name under the temporary folder.
newFolder()
          Returns a new fresh folder with a random name under the temporary folder.
newFolder(String... paths)
          Returns a new fresh folder with the given paths under the temporary folder.
newFolder(String path)
          Returns a new fresh folder with the given path 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
 

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.

TemporaryFolder

protected TemporaryFolder(TemporaryFolder.Builder builder)
Create a TemporaryFolder initialized with values from a builder.

builder

public static TemporaryFolder.Builder builder()
Returns a new builder for building an instance of TemporaryFolder.
Since:
4.13

before

protected void before()
 throws Throwable
Description copied from class: ExternalResource
Override to set up your specific external resource.
Overrides:
before in class ExternalResource
Throws:
Throwable - if setup fails (which will disable after

after

protected void after()
Description copied from class: ExternalResource
Override to tear down your specific external resource.
Overrides:
after in class ExternalResource

create

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

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.


Copyright © 2002–2021 JUnit. All rights reserved.