public class TemporaryFolder
extends org.junit.rules.ExternalResource
TemporaryFolder
which looks different in different junit versions and does not reset
the private field folder on the after() event.
Another change is that the folder is only created on demand.
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 |
|---|---|
protected void |
addSourceInfo(File tempDir)
generate '.createdBy' file with a stack trace so tests that leak temp folders can be identified later.
|
protected void |
after() |
protected void |
before() |
void |
create()
for testing purposes only.
|
File |
createTempFile(String prefix,
String suffix)
Same signature as
File.createTempFile(String, String). |
void |
delete()
Delete all files and folders under the temporary folder.
|
File |
getRoot() |
boolean |
isInitialized() |
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... folderNames)
Returns a new fresh folder with the given name under the temporary folder.
|
protected void before()
throws Throwable
before in class org.junit.rules.ExternalResourceThrowableprotected void after()
after in class org.junit.rules.ExternalResourcepublic boolean isInitialized()
public void create()
throws IOException
IOExceptionpublic File newFile(String fileName) throws IOException
IOExceptionpublic File createTempFile(String prefix, String suffix) throws IOException
File.createTempFile(String, String).IOExceptionpublic File newFile() throws IOException
IOExceptionpublic File newFolder(String... folderNames)
public File newFolder() throws IOException
IOExceptionprotected void addSourceInfo(File tempDir) throws IOException
IOExceptionpublic File getRoot()
public void delete()
RuleCopyright © 2016. All Rights Reserved.