The built-in TempDirectory extension is mainly used to create and cleanup a temporary directory for an individual test or all tests in a test class.
To use it, We have to annotate a non-private field of type java.io.File Or java.nio.file.Path with @TempDir or add a parameter of type java.io.File Or java.nio.file.Path annotated with @TempDir to a lifecycle method or test method.
For example, the following test declares a parameter annotated with @TempDir for a single test method, creates and writes to a file in the temporary directory, and checks its content.
1 2 3 4 5 6 7 8 9 10 11 12 |
@Test void testWithTempDir(@TempDir Path tempDir) throws IOException { Path numbersPath = tempDir.resolve("numberstest.txt"); List<String> numberList = Arrays.asList("100", "200", "300"); Files.write(numbersPath, numberList); assertAll( () -> assertTrue(Files.exists(numbersPath),"File should exist"), () -> assertLinesMatch(numberList, Files.readAllLines(numbersPath))); } |
Key Points:
@TempDir is not supported on constructor parameters. If you wish to retain a single reference to a temp directory across lifecycle methods and the current test method, please use field injection, by annotating a non-private instance field with @TempDir.
Let’s try to understand the above concept using a demo project
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>JUnit5TempDirectoryExtensionExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> <junit.jupiter.version>5.6.2</junit.jupiter.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
SharedTempDirectoryTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertLinesMatch; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; /** * @TempDir(TempDirectory Extension) example in JUnit 5 * @author KK JavaTutorials */ public class SharedTempDirectoryTest { @TempDir static Path sharedTempDir; @BeforeAll static void beforeAll() { System.out.println(sharedTempDir); } /** * @TempDir as a Method Parameter * @param tempDir * @throws IOException */ @Test void testWithTempDir(@TempDir Path tempDir) throws IOException { Path numbersPath = tempDir.resolve("numberstest.txt"); List<String> numberList = Arrays.asList("100", "200", "300"); Files.write(numbersPath, numberList); assertAll( () -> assertTrue(Files.exists(numbersPath),"File should exist"), () -> assertLinesMatch(numberList, Files.readAllLines(numbersPath))); } /** * @TempDir on an Instance Field * @throws IOException */ @Test void testAsSharedTempDir1() throws IOException { Path numbersPath = sharedTempDir.resolve("mynumbers.txt"); List<String> numberList = Arrays.asList("10", "20", "30"); Files.write(numbersPath, numberList); assertAll( () -> assertTrue(Files.exists(numbersPath),"File should exist"), () -> assertLinesMatch(numberList, Files.readAllLines(numbersPath))); } /** * @TempDir on an Instance Field * @throws IOException */ @Test void testAsSharedTempDir2() throws IOException { Path path = sharedTempDir.resolve("names.txt"); List<String> nameList = Arrays.asList("KK", "PK", "SK"); Files.write(path, nameList); assertLinesMatch(nameList, Files.readAllLines(path)); } @AfterAll static void afterAll() { System.out.println(sharedTempDir.getFileName()); } } |
The output of the above project:
You May Also Like:
Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5
That’s all about JUnit 5 Temporary Directory Support
If you have any feedback or suggestion please feel free to drop in below comment box.