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 |
after() |
protected void |
before() |
void |
create()
for testing purposes only.
|
java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix)
Same signature as
File.createTempFile(String, String) . |
void |
delete()
Delete all files and folders under the temporary folder.
|
java.io.File |
getRoot() |
boolean |
isInitialized() |
java.io.File |
newFile()
Returns a new fresh file with a random name under the temporary folder.
|
java.io.File |
newFile(java.lang.String fileName)
Returns a new fresh file with the given name under the temporary folder.
|
java.io.File |
newFolder()
Returns a new fresh folder with a random name under the temporary
folder.
|
java.io.File |
newFolder(java.lang.String... folderNames)
Returns a new fresh folder with the given name under the temporary folder.
|
protected void before() throws java.lang.Throwable
before
in class org.junit.rules.ExternalResource
java.lang.Throwable
protected void after()
after
in class org.junit.rules.ExternalResource
public boolean isInitialized()
public void create() throws java.io.IOException
java.io.IOException
public java.io.File newFile(java.lang.String fileName) throws java.io.IOException
java.io.IOException
public java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
File.createTempFile(String, String)
.java.io.IOException
public java.io.File newFile() throws java.io.IOException
java.io.IOException
public java.io.File newFolder(java.lang.String... folderNames)
public java.io.File newFolder() throws java.io.IOException
java.io.IOException
public java.io.File getRoot()
public void delete()
Rule