JUnit 4.7 – TemporaryFolder Core Rule

JUnit 4.7 has been ‘secretly’ released! Continuing my look at the new features I have had a dabble into the functionality provided by the TemporaryFolder Rule.

The TemporaryFolder rule is used to have all files or directories created using it deleted once the test has completed.

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

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

import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class JUnitCoreTemporaryFolderRuleTest {

  private static File tempFile;
  private static File nonTempFile;
  private static File tempDir;
  private static File nonTempDir;

  // TemporaryFolder core rule enables the creation of files and folders that
  // are guaranteed to be deleted when the test method finishes
  @Rule
  public TemporaryFolder tempFolder = new TemporaryFolder();

  @Test
  public void testFileCreation() throws IOException {
    tempFile = tempFolder.newFile("temp.txt");
    nonTempFile = new File("non-temp.txt");
    nonTempFile.createNewFile();
    assertTrue(tempFile.exists());
    assertTrue(nonTempFile.exists());
  }

  @Test
  public void testDirectoryCreation() throws IOException {
    tempDir = tempFolder.newFolder("tempDir");
    nonTempDir = new File("nonTempDir");
    nonTempDir.mkdir();
    assertTrue(tempDir.exists());
    assertTrue(nonTempDir.exists());
  }

  @AfterClass
  public static void testNoTemporaryFolderFileExists() {
    assertFalse(tempFile.exists());
  }

  @AfterClass
  public static void testNonTemporaryFolderFileStillExists() {
    assertTrue(nonTempFile.exists());
    nonTempFile.delete();
    assertFalse(nonTempFile.exists());
  }

  @AfterClass
  public static void testNoTemporaryFolderDirectoryExists() {
    assertFalse(tempDir.exists());
  }

  @AfterClass
  public static void testNonTemporaryFolderDirectoryStillExists() {
    assertTrue(nonTempDir.exists());
    nonTempDir.delete();
    assertFalse(nonTempDir.exists());
  }
}

This simple test confirms the expected behaviour of the TemporaryFolder rule. The temporary file and folder don’t exist once the test has run, whilst the file and folder not creating using the TemporaryFolder rule do exist after the test runs.

This rule is useful for tests methods that create files or directories without then having to manage any tidy up once the test completes.

Next up I will take a look at the ExternalResource core rule.

I have also been creating a JUnit Goodness presentation lately featuring Parameterized Tests, assertThat() with matchers and the @Ignore annotation, so will look to post that info some time soon too…probably after trying out the new features/updates/fixes in 4.7.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter
  • Add to favorites

Tags: , ,

Comments are closed.