Junit @TempDir annotation

In this post under Junit, I will show with example the purpose and how to use @TempDir annotation.

Lets say you have written class that zip the input file as shown below

FileZip

package package20;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileZip {
public void zip(File inputFile) throws Exception {
String zipFileName = inputFile.getName().substring(0, inputFile.getName().lastIndexOf("."));
File zipFile = new File(inputFile.getParent(), zipFileName + ".zip");
try(FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
ZipEntry zipEntry = new ZipEntry(inputFile.getName());
zipOutputStream.putNextEntry(zipEntry);
try(FileInputStream fileInputStream = new FileInputStream(inputFile)) {
byte[] data = new byte[1024];
int bytesRead = fileInputStream.read(data);
while(bytesRead != -1) {
zipOutputStream.write(data, 0, bytesRead);
bytesRead = fileInputStream.read(data);
}
zipOutputStream.closeEntry();
fileInputStream.close();
}
}
}
}

The above method takes an input file say “source.txt” and creates a zip file “source.zip” in the same folder where the source file is present.

Now we have to test it. So below are the steps that we perform
1) Add a method that is annotated with @BeforeEach. In this method we will create a temporary directory, temporary file. Populate the temporary file with some contents
2) Add the test method annotated with @Test and add the code that will test the method using the file created in step1.
3) Add a method that is annotated with @AfterEach. In this method we will delete the file and directory after execution of test.

JUnit observed that step1 and step3 will be performed by everyone who want to use temporary files and directories in there test cases. Thereby replicating the same code everywhere.

So JUnit came up with this @TempDir feature to prevent people from replicating the steps again and again.

JUnit will take care of step1 and step2. We just need to worry about the actual test method.

All we have to do is create a property of type java.io.File or java.nio.Path in the test class and annotate it with “@TempDir” annotation.

Please note the property can be a static field, instance field, method parameter, parameter in “@BeforeEach” and “@AfterEach” method, or parameter in “@BeforeAll” method

This “@TempDir” annotation will create a temporary directory and in that temporary directory we create any number of our own directory or file. And by the end of test execution, JUnit will delete the temporary directory and its childrens.

Below is the test class showing how to use it

Test class

package package20;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class FileZipTest {
@TempDir
private File tempDir;
@Test
public void zipTest() throws Exception {
File inputFile = new File(tempDir.getAbsolutePath() + "input.txt");
Files.writeString(inputFile.toPath(), "Hello I am about to be zipped");
FileZip fileZip = new FileZip();
fileZip.zip(inputFile);
File outputFile = new File(tempDir.getAbsolutePath() + "input.zip");
assertTrue(outputFile.exists());
}
}

In the above class, at line 13, I created an instance field “tempDir” and annotated it with “@TempDir” annotation.

Now JUnit will create a temporary directory and assigns the path to that directory to this field “tempDir”.

Now in the “zipTest” method, at line 17, I create a file “input.txt” in the same temporary directory “tempDir” created by Junit.

At line 18, I save some contents in the file.

At line 20, I call “zip” method.

At line 22, I assert that a file named “input.zip” is created inside the “tempDir”.

After this test case is executed, JUnit will automatically delete the temporary directory.

In this way we can use “@TempDir” annotation.

Leave a comment