I'm migrating unit tests from Junit4 to Junit5. In the test I'm using TemporaryFolder rule from Junit4 API. To keep the test working I added @EnableRuleMigrationSupport annotation:
@EnableRuleMigrationSupport
public final class SomeTest {
@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
// tests ...
}
As I understand, in Junit5 I need to use extensions instead of rules, but I can't find any replacement for TemporaryFolder in Junit5 extensions. Does it exist? How to correctly replace TemporaryFolder rule with extension?
You can use the @TempDir annotation (JUnit 5.4+), described in §2.20.1 of the JUnit 5 User Guide. From the user guide (emphasis mine):
The built-in
TempDirectoryextension is used to create and clean up a temporary directory for an individual test or all tests in a test class. It is registered by default. To use it, annotate a non-private field of typejava.nio.file.Pathorjava.io.Filewith@TempDiror add a parameter of typejava.nio.file.Pathorjava.io.Fileannotated with@TempDirto a lifecycle method or test method.
Note: This extension was added in version 5.4 and is currently (as of 5.7.2) experimental.
Example of using an instance field:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path; // or use java.io.File
class SomeTests {
@TempDir
Path directory; // must be non-private
}
Example of using a parameter of a test method:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path; // or use java.io.File
class SomeTests {
@Test
void testFoo(@TempDir Path directory) {
// do test...
}
}
Note: Constructor parameters are not supported.
When the directory is created and deleted is described in the Javadoc of @TempDir:
Temporary Directory Creation
The temporary directory is only created if a field in a test class or a parameter in a lifecycle method or test method is annotated with
@TempDir. If the field type or parameter type is neitherPathnorFileor if the temporary directory cannot be created, anExtensionConfigurationExceptionor aParameterResolutionExceptionwill be thrown as appropriate. In addition, aParameterResolutionExceptionwill be thrown for a constructor parameter annotated with@TempDir.Temporary Directory Scope
The scope of the temporary directory depends on where the first
@TempDirannotation is encountered when executing a test class. The temporary directory will be shared by all tests in a class when the annotation is present on astaticfield or on a parameter of a@BeforeAllmethod. Otherwise — for example, when@TempDiris only used on instance fields or on parameters in test,@BeforeEach, or@AfterEachmethods — each test will use its own temporary directory.Temporary Directory Deletion
When the end of the scope of a temporary directory is reached, i.e. when the test method or class has finished execution, JUnit will attempt to recursively delete all files and directories in the temporary directory and, finally, the temporary directory itself. In case deletion of a file or directory fails, an
IOExceptionwill be thrown that will cause the test or test class to fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With