Creating temporary files or folders in Junit

17:03

Numerous times we write Junit tests where we need to create temporary files and folders for executing the testcase. Usually, we create temporary folder at some physical location and write temporary files in it. Major pain in this approach is that we need to take care of creation and cleanup of all such files during the test lifecycle.

Junit4 comes with special Rule TemporaryFolder which automatically takes care of all such things. It creates the temp folder when a test starts and deletes the temp folder when the test finishes. It does this for every test method in the class. It will always clean up the files, whether the test passes, fails, or throws an exception.

Under the hood, TemporaryFolder uses the File.createTempFile() method to create the folder, so it's storing the folder in operating system's temp folder. It also assigns a unique name to the folder, so if the JVM crashes and TemporaryFolder does not clean up your files, the results of next test run will not be skewed by the files from the previous run.

Let’s see how it works:
package blog.techypages.junit.test;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import blog.techypages.rule.RepeatRule;

/**
 * Class to check some of the usage of @see TemporaryFolder
 * 
 * @author abhishek
 *
 */
public class TemporaryFolderTest {
    @Rule
    public RepeatRule repeat = new RepeatRule(10);
    
    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder();

    @Test
    public void sampleTest() throws IOException {
        String writeString = "The quick brown fox jumps over the lazy dog.";
        
        // 1. Create a temp file.
        File tmpFile = tempFolder.newFile();
        System.out.println("Path: " + tmpFile.getAbsolutePath());

        // 2. Write writeString to this temp file.
        FileUtils.writeStringToFile(tmpFile, writeString);

        // 3. Read content of the temp file
        String readString = FileUtils.readFileToString(tmpFile);

        // 4. Verify: readString and writeString should be equal.
        Assert.assertEquals(writeString, readString);
    }

}


Output:
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit17809417054081844.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit744343614217417992.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit5848675023725375369.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit3428415497682180161.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit7729225341687093633.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit8396781193866390009.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit7982918886982845418.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit986976126465928150.tmp
Path: C:\Users\ABHISH~1\AppData\Local\Temp\junit8242036959618829704\junit7979718795863279044.tmp

Note: It’s clearly evident from the output that on each repetition newFile() creates a new file within the temporary directory with a random file name[If a file name is not passed into the method, then it will generate a random file name]. Here, we used RepeatRule(10) to repeat the same testcase 10 times. For more information on RepeatRule, please read the previous post “Repeating a test case in Junit”.

Now after the test execution, let’s do a quick look up for the temp folder "junit8242036959618829704" in the system’s temp folder to see whether it exists or not:
C:\Users\ABHISH~1\AppData\Local\Temp>ls | grep "junit8242036959618829704"

C:\Users\ABHISH~1\AppData\Local\Temp>

You can see that the temp folder "junit8242036959618829704" has been cleaned up by TemporaryFolder.

It’s really very useful feature of Junit, which takes out the pain involved with temp files management in unit testing.

Hope this article helped. Happy learning... :)


You Might Also Like

1 comments

  1. I really like what you guys are up too. This sort of clever work and exposure! Keep up the fantastic works guys I've added you guys to our blogroll. capital one credit card login

    ReplyDelete