Sub topic 13
Testing File Code with @TempDir
Some code reads and writes files. Testing that code takes some care. If a test writes into your real project folder, it can leave junk files behind. It can even overwrite something important. JUnit 5 solves this with @TempDir.
What a temporary directory is
A temporary directory is a folder that exists only for a short time. JUnit creates it before your test runs and deletes it afterwards, along with every file inside it.
@TempDir is an annotation, which is a label you put on code to give it extra meaning. When JUnit sees @TempDir on a field or a parameter of type Path, it fills it in with a new, empty temporary folder. A Path is Java's way of pointing to a file or folder location.
A first example
This test writes a file and reads it back. All of it happens inside the temporary folder.
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class FileWritingTest {
@TempDir
Path tempDir;
@Test
void writesAndReadsFile() throws IOException {
Path file = tempDir.resolve("note.txt");
Files.writeString(file, "hello");
assertEquals("hello", Files.readString(file));
}
}Here is what each part does:
@TempDir Path tempDirasks JUnit for a fresh folder.tempDir.resolve("note.txt")builds a path to a file namednote.txtinside that folder. The file does not exist yet.Files.writeStringcreates the file and writes the text into it.assertEqualschecks that the file holds the text you expect.
You never delete anything yourself. JUnit cleans up the folder after the test.
Testing your own class
In a real project, the code under test is usually your own class. Pass the temporary folder into it, so the class never touches real files.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class NoteSaver {
private final Path folder;
public NoteSaver(Path folder) {
this.folder = folder;
}
public Path save(String name, String text) throws IOException {
Path file = folder.resolve(name + ".txt");
Files.writeString(file, text);
return file;
}
}You can also put @TempDir on a test method's parameter instead of a field:
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class NoteSaverTest {
@Test
void saveCreatesTextFile(@TempDir Path folder) throws IOException {
// Arrange
NoteSaver saver = new NoteSaver(folder);
// Act
Path saved = saver.save("todo", "buy milk");
// Assert
assertTrue(Files.exists(saved));
assertEquals("buy milk", Files.readString(saved));
}
}Why each test gets its own folder
When @TempDir is on an instance field, each test method gets a new folder. A file written by one test cannot affect another test. This keeps your tests independent, which means they pass or fail the same way no matter what order they run in.
Exercise
A test uses a @TempDir field and writes a file named data.txt. A second test in the same class checks that data.txt does not exist. Does the second test pass? Why?
Show answer
Yes. With a non-static @TempDir field, JUnit gives each test method its own new, empty folder and deletes it after the test, so the second test never sees the first test's file.
Exercise
Why is it better to pass a Path into NoteSaver's constructor instead of hard-coding a folder like "C:/notes" inside the class?
Show answer
A tests can then pass a @TempDir folder, so they never write into real folders, and production code can still pass whatever real folder it needs.