Package org.junit.rules
Class TemporaryFolder
java.lang.Object
org.junit.rules.ExternalResource
org.junit.rules.TemporaryFolder
- All Implemented Interfaces:
TestRule
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
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreate 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.protected
TemporaryFolder
(TemporaryFolder.Builder builder) Create aTemporaryFolder
initialized with values from a builder. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
Override to tear down your specific external resource.protected void
before()
Override to set up your specific external resource.static TemporaryFolder.Builder
builder()
Returns a new builder for building an instance ofTemporaryFolder
.void
create()
for testing purposes only.private static File
createTemporaryFolderIn
(File parentFolder) private static File
createTemporaryFolderWithFileApi
(File parentFolder) private static File
createTemporaryFolderWithNioApi
(File parentFolder) void
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.Returns a new fresh file with the given name under the temporary folder.Returns a new fresh folder with a random name under the temporary folder.Returns a new fresh folder with the given path under the temporary folder.Returns a new fresh folder with the given paths under the temporary folder.private boolean
recursiveDelete
(File file) private boolean
Tries to delete all files and folders under the temporary folder and returns whether deletion was successful or not.Methods inherited from class org.junit.rules.ExternalResource
apply
-
Field Details
-
parentFolder
-
assureDeletion
private final boolean assureDeletion -
folder
-
TEMP_DIR_ATTEMPTS
private static final int TEMP_DIR_ATTEMPTS- See Also:
-
TMP_PREFIX
- See Also:
-
-
Constructor Details
-
TemporaryFolder
public TemporaryFolder()Create a temporary folder which uses system default temporary-file directory to create temporary resources. -
TemporaryFolder
Create a temporary folder which uses the specified directory to create temporary resources.- Parameters:
parentFolder
- folder where temporary resources will be created. Ifnull
then system default temporary-file directory is used.
-
TemporaryFolder
Create aTemporaryFolder
initialized with values from a builder.
-
-
Method Details
-
builder
Returns a new builder for building an instance ofTemporaryFolder
.- Since:
- 4.13
-
before
Description copied from class:ExternalResource
Override to set up your specific external resource.- Overrides:
before
in classExternalResource
- Throws:
Throwable
- if setup fails (which will disableafter
-
after
protected void after()Description copied from class:ExternalResource
Override to tear down your specific external resource.- Overrides:
after
in classExternalResource
-
create
for testing purposes only. Do not use.- Throws:
IOException
-
newFile
Returns a new fresh file with the given name under the temporary folder.- Throws:
IOException
-
newFile
Returns a new fresh file with a random name under the temporary folder.- Throws:
IOException
-
newFolder
Returns a new fresh folder with the given path under the temporary folder.- Throws:
IOException
-
newFolder
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
Returns a new fresh folder with a random name under the temporary folder.- Throws:
IOException
-
createTemporaryFolderIn
- Throws:
IOException
-
createTemporaryFolderWithNioApi
private static File createTemporaryFolderWithNioApi(File parentFolder) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException -
createTemporaryFolderWithFileApi
- Throws:
IOException
-
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 theRule
.- Throws:
AssertionError
- if unable to clean up resources and deletion of resources is assured.
-
tryDelete
private boolean tryDelete()Tries to delete all files and folders under the temporary folder and returns whether deletion was successful or not.- Returns:
true
if all resources are deleted successfully,false
otherwise.
-
recursiveDelete
-