|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.junit.rules.ExternalResource org.junit.rules.TemporaryFolder
public class TemporaryFolder
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();
Nested Class Summary | |
---|---|
static class |
TemporaryFolder.Builder
Builds an instance of TemporaryFolder . |
Constructor Summary | |
---|---|
|
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. |
protected |
TemporaryFolder(TemporaryFolder.Builder builder)
Create a TemporaryFolder initialized with
values from a builder. |
Method Summary | |
---|---|
protected 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 of TemporaryFolder . |
void |
create()
for testing purposes only. |
void |
delete()
Delete all files and folders under the temporary folder. |
File |
getRoot()
|
File |
newFile()
Returns a new fresh file with a random name under the temporary folder. |
File |
newFile(String fileName)
Returns a new fresh file with the given name under the temporary folder. |
File |
newFolder()
Returns a new fresh folder with a random name under the temporary folder. |
File |
newFolder(String... paths)
Returns a new fresh folder with the given paths under the temporary folder. |
File |
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 |
Constructor Detail |
---|
public TemporaryFolder()
public TemporaryFolder(File parentFolder)
parentFolder
- folder where temporary resources will be created.
If null
then system default temporary-file directory is used.protected TemporaryFolder(TemporaryFolder.Builder builder)
TemporaryFolder
initialized with
values from a builder.
Method Detail |
---|
public static TemporaryFolder.Builder builder()
TemporaryFolder
.
protected void before() throws Throwable
ExternalResource
before
in class ExternalResource
Throwable
- if setup fails (which will disable after
protected void after()
ExternalResource
after
in class ExternalResource
public void create() throws IOException
IOException
public File newFile(String fileName) throws IOException
IOException
public File newFile() throws IOException
IOException
public File newFolder(String path) throws IOException
IOException
public File newFolder(String... paths) throws IOException
"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.
IOException
public File newFolder() throws IOException
IOException
public File getRoot()
public void delete()
Rule
.
AssertionError
- if unable to clean up resources
and deletion of resources is assured.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |