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 {
    public TemporaryFolder testFolder = new TemporaryFolder();

    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.


5 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?


  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.


  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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s