JUnit Tip: Use rules to manage temporary files and folders

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.

10 thoughts on “JUnit Tip: Use rules to manage temporary files and folders

  1. Pieter

    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?

    Like

    Reply
  2. Gary Gregory

    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.

    Like

    Reply
  3. Pingback: Solved: Temporary File Flood in JUnit Tests in Maven Build | Minds

  4. Pingback: What does @Rule do? | Solutions for enthusiast and professional programmers

  5. Pingback: What does @Rule do? - HTML CODE

  6. Pingback: What is the correct way to write to temp file during unit tests with Maven? – Java

  7. Pingback: What is the correct way to write to temp file during unit tests with Maven? - Tutorial Guruji

  8. Pingback: What is the correct way to write to temp file during unit tests with Maven? - PhotoLens

  9. Pingback: Testing Realm under Android

  10. Pingback: What is the correct way to write to temp file during unit tests with Maven? – Row Coding

Leave a comment