public class TemporaryFolder extends Object implements org.junit.rules.MethodRule
Why we have our own version of this class: http://stackoverflow.com/questions/7738881/junit-rule-lifecycle-interaction-with-before
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");
// ...
}
}
| Constructor and Description |
|---|
TemporaryFolder() |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target) |
void |
create()
For testing purposes only.
|
void |
delete()
Delete all files and folders under the temporary folder.
|
File |
getRoot() |
File |
newFile(String first,
String... others)
Returns a new fresh file with the given name under the temporary folder.
|
File |
newFolder(String first,
String... others)
Returns a new fresh folder with the given name under the temporary folder.
|
public final org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runners.model.FrameworkMethod method,
Object target)
apply in interface org.junit.rules.MethodRulepublic void create()
throws IOException
IOException - if a file operation fails.public File newFile(String first, String... others) throws IOException
first - The first path element.others - Any other path elements.IOException - if a file operation fails.public File newFolder(String first, String... others)
first - The first path element.others - Any other path elements.public File getRoot()
public void delete()
RuleCopyright © 2016 Last.fm. All Rights Reserved.