0
0
JunitHow-ToBeginner ยท 3 min read

How to Use @TempDir in JUnit for Temporary Folder Management

Use the @TempDir annotation in JUnit 5 to inject a temporary directory as a Path or File into your test method or field. This directory is created fresh for each test and automatically deleted after the test finishes, helping you manage temporary files safely.
๐Ÿ“

Syntax

The @TempDir annotation can be applied to a Path or File parameter in a test method or to a non-private field in a test class. JUnit will create a new temporary directory and inject it automatically.

  • @TempDir: Marks the temporary directory injection point.
  • Path or File: The type of the injected temporary directory.
  • Test method parameter or field: Where the temporary directory is injected.
java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;

class TempDirSyntaxExample {

    @Test
    void testWithTempDir(@TempDir Path tempDir) {
        // tempDir is a fresh temporary directory
    }

    @TempDir
    Path tempDirField;

    @Test
    void testUsingField() {
        // tempDirField is available here
    }
}
๐Ÿ’ป

Example

This example shows how to use @TempDir to create a temporary directory, write a file inside it, and verify the file exists. The temporary directory and its contents are deleted after the test completes.

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.*;

class TempDirExample {

    @Test
    void createFileInTempDir(@TempDir Path tempDir) throws IOException {
        Path tempFile = tempDir.resolve("testfile.txt");
        String content = "Hello, JUnit!";
        Files.writeString(tempFile, content);

        assertTrue(Files.exists(tempFile), "File should exist");
        assertEquals(content, Files.readString(tempFile), "File content should match");
    }
}
Output
Test passed
โš ๏ธ

Common Pitfalls

  • Do not use @TempDir on private fields; it must be package-private, protected, or public.
  • Remember that the temporary directory is deleted after each test, so do not rely on it persisting between tests.
  • Do not manually delete the directory inside the test; JUnit handles cleanup automatically.
  • Using @TempDir in static methods or static fields is not supported.
java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Path;

class TempDirPitfall {

    // Wrong: private field will not be injected
    // @TempDir
    // private Path tempDirPrivate;

    // Correct:
    @TempDir
    Path tempDir;

    @Test
    void test() {
        // tempDir is injected correctly here
    }
}
๐Ÿ“Š

Quick Reference

Summary tips for using @TempDir in JUnit:

  • Annotate a Path or File parameter or field with @TempDir.
  • Use non-private fields or method parameters only.
  • JUnit creates and cleans the directory automatically.
  • Use the temporary directory to safely create files or folders during tests.
โœ…

Key Takeaways

Use @TempDir to get a fresh temporary directory automatically for each test.
Inject @TempDir as a Path or File parameter or a non-private field.
JUnit cleans up the temporary directory after the test finishes.
Avoid private or static fields with @TempDir to ensure proper injection.
Use the temporary directory to safely create and test files without manual cleanup.