You can remove the burden of deleting temporary files and folders from your test code by using a JUnit TemporaryFolder Rule. Rules themselves are new in JUnit 4.7 and are used to change the behavior of tests. For example, the following test creates and deletes a temporary file and folder:
package test; import java.io.File; import java.io.IOException; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; public class TestTemporaryFolderRule { @Rule public TemporaryFolder testFolder = new TemporaryFolder(); @Test public void testInTempFolder() throws IOException { File tempFile = testFolder.newFile("file.txt"); File tempFolder = testFolder.newFolder("folder"); System.out.println("Test folder: " + testFolder.getRoot()); // test... } }
In order to enable this feature, you must use the annotation @Rule on the declaration of the TemporaryFolder instance variable. The TemporaryFolder creates a folder in the default temporary file directory specified by the system property java.io.tmpdir. The method newFile creates a new file in the temporary directory and newFolder creates a new folder.
When the test method finishes, JUnit automatically deletes all files and directories in and including the TemporaryFolder. JUnit guarantees to delete the resources, whether the test passes or fails.
Thank you for the example and good explanation
This seems very useful and relieves the developer from the burden of managing the files & directories, but wouldn’t you want the temporary files & folders to remain if the test failed such that you could examine them after an error?
LikeLike
The TemporaryFolder Rule cleans the folder, whether the test passes or not, that is the contract for the rule.
Here are some alternatives:
– Make sure your failure messages includes enough information such that you do not need to look at the temporary files. This may require catching and re-throwing exceptions.
– Do not use the rule, instead, use try-catch-finally blocks where the try block creates the temporary files and implements the test, the catch block backs up files for a post-mortem and the finally block cleans up the temporary folder. After you write a couple of these, you can refactor the block elements into methods to avoid repetition. Refactor further into a super class for re-use between test classes.
– Do use the rule but detect the error or catch an exception and back up the files needed for a post-mortem. This lets the rule clean up the folder if the test is successful. If the test fails, the rule still cleans the folder after your back up code runs.
LikeLike
Pingback: Solved: Temporary File Flood in JUnit Tests in Maven Build | Minds
Pingback: What does @Rule do? | Solutions for enthusiast and professional programmers
Pingback: What does @Rule do? - HTML CODE
Pingback: What is the correct way to write to temp file during unit tests with Maven? – Java
Pingback: What is the correct way to write to temp file during unit tests with Maven? - Tutorial Guruji
Pingback: What is the correct way to write to temp file during unit tests with Maven? - PhotoLens
Pingback: Testing Realm under Android
Pingback: What is the correct way to write to temp file during unit tests with Maven? – Row Coding