public class

TemporaryFolder

extends ExternalResource
java.lang.Object
   ↳ org.junit.rules.ExternalResource
     ↳ org.junit.rules.TemporaryFolder

Class Overview

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

Summary

Public Constructors
TemporaryFolder()
Public Methods
void create()
for testing purposes only.
void delete()
Delete all files and folders under the temporary folder.
File getRoot()
File newFile(String fileName)
Returns a new fresh file with the given name under the temporary folder.
File newFolder(String folderName)
Returns a new fresh folder with the given name under the temporary folder.
Protected Methods
void after()
Override to tear down your specific external resource.
void before()
Override to set up your specific external resource.
[Expand]
Inherited Methods
From class org.junit.rules.ExternalResource
From class java.lang.Object
From interface org.junit.rules.MethodRule

Public Constructors

public TemporaryFolder ()

Public Methods

public void create ()

for testing purposes only. Do not use.

Throws
IOException

public void delete ()

Delete all files and folders under the temporary folder. Usually not called directly, since it is automatically applied by the Rule

public File getRoot ()

Returns
  • the location of this temporary folder.

public File newFile (String fileName)

Returns a new fresh file with the given name under the temporary folder.

Throws
IOException

public File newFolder (String folderName)

Returns a new fresh folder with the given name under the temporary folder.

Protected Methods

protected void after ()

Override to tear down your specific external resource.

protected void before ()

Override to set up your specific external resource.

Throws
Throwable