Package org.junit.rules
Class TemporaryFolder
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- 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). Whether the deletion is successful or not is not checked by this rule. 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"); // ... } }- Since:
- 4.7
-
-
Constructor Summary
Constructors Constructor Description TemporaryFolder()TemporaryFolder(java.io.File parentFolder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafter()Override to tear down your specific external resource.protected voidbefore()Override to set up your specific external resource.voidcreate()for testing purposes only.voiddelete()Delete all files and folders under the temporary folder.java.io.FilegetRoot()java.io.FilenewFile()Returns a new fresh file with a random name under the temporary folder.java.io.FilenewFile(java.lang.String fileName)Returns a new fresh file with the given name under the temporary folder.java.io.FilenewFolder()Returns a new fresh folder with a random name under the temporary folder.java.io.FilenewFolder(java.lang.String folder)Returns a new fresh folder with the given name under the temporary folder.java.io.FilenewFolder(java.lang.String... folderNames)Returns a new fresh folder with the given name(s) under the temporary folder.-
Methods inherited from class org.junit.rules.ExternalResource
apply
-
-
-
-
Method Detail
-
before
protected void before() throws java.lang.ThrowableDescription copied from class:ExternalResourceOverride to set up your specific external resource.- Overrides:
beforein classExternalResource- Throws:
java.lang.Throwable- if setup fails (which will disableafter
-
after
protected void after()
Description copied from class:ExternalResourceOverride to tear down your specific external resource.- Overrides:
afterin classExternalResource
-
create
public void create() throws java.io.IOExceptionfor testing purposes only. Do not use.- Throws:
java.io.IOException
-
newFile
public java.io.File newFile(java.lang.String fileName) throws java.io.IOExceptionReturns a new fresh file with the given name under the temporary folder.- Throws:
java.io.IOException
-
newFile
public java.io.File newFile() throws java.io.IOExceptionReturns 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 folder) throws java.io.IOExceptionReturns a new fresh folder with the given name under the temporary folder.- Throws:
java.io.IOException
-
newFolder
public java.io.File newFolder(java.lang.String... folderNames) throws java.io.IOExceptionReturns a new fresh folder with the given name(s) under the temporary folder.- Throws:
java.io.IOException
-
newFolder
public java.io.File newFolder() throws java.io.IOExceptionReturns a new fresh folder with a random name under the temporary folder.- 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 theRule
-
-