public class TemporaryFolderExtension extends Object implements org.junit.jupiter.api.extension.ParameterResolver
TemporaryFolder which you can use to create a
temporary file or directory for use by your test. The TemporaryFolder can be injected
into your test or test case with any of the following approaches:
@BeforeEach method. The TemporaryFolder
will be destroyed during @AfterEach and no exception will be thrown in cases where
the deletion fails. For example:
private TemporaryFolder temporaryFolder;
@BeforeEach
public void setUp(TemporaryFolder temporaryFolder) {
this.temporaryFolder = temporaryFolder
// ...
}
@Test method. The TemporaryFolder will be
destroyed during @AfterEach and no exception will be thrown in cases where the
deletion fails. For example:
@Test
public void testUsingTemporaryFolder(TemporaryFolder temporaryFolder) {
// ...
}
@BeforeAll method. Note: in this case all
tests in the test case will share the same instance of the TemporaryFolder. The
TemporaryFolder will be destroyed after any @AfterAll method completes and
no exception will be thrown in cases where the deletion fails. For example:
private static TemporaryFolder TEMPORARY_FOLDER;
@BeforeAll
public static void setUp(TemporaryFolder givenTemporaryFolder) {
TEMPORARY_FOLDER = givenTemporaryFolder
// ...
}
Usage examples:
Injecting a TemporaryFolder in a @BeforeEach method:
@ExtendWith(TemporaryFolderExtension.class)
public class MyTest {
private TemporaryFolder temporaryFolder;
@BeforeEach
public void setUp(TemporaryFolder temporaryFolder) {
this.temporaryFolder = temporaryFolder
// ...
}
@Test
public void testUsingTemporaryFile() {
File file = temporaryFolder.createFile("foo.txt");
// ...
}
@Test
public void testUsingTemporaryDirectory() {
// use the temporary folder itself
File root = temporaryFolder.getRoot();
// create a sub directory within the temporary folder
File file = temporaryFolder.createDirectory("foo");
// ...
}
}
Injecting a TemporaryFolder in a @Test method:
public class MyTest {
@Test
@ExtendWith(TemporaryFolderExtension.class)
public void testUsingTemporaryFile(TemporaryFolder temporaryFolder) {
File file = temporaryFolder.createFile("foo.txt");
// ...
}
@Test
@ExtendWith(TemporaryFolderExtension.class)
public void testUsingTemporaryDirectory(TemporaryFolder temporaryFolder) {
// use the temporary folder itself
File root = temporaryFolder.getRoot();
// create a sub directory within the temporary folder
File file = temporaryFolder.createDirectory("foo");
// ...
}
}
| Constructor and Description |
|---|
TemporaryFolderExtension() |
| Modifier and Type | Method and Description |
|---|---|
Object |
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Provides a value for any parameter context which has passed the
supportsParameter(ParameterContext, ExtensionContext) gate. |
boolean |
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Does this extension support injection for parameters of the type described by the given
parameterContext? |
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext)
throws org.junit.jupiter.api.extension.ParameterResolutionException
parameterContext?supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolverparameterContext - the context for the parameter for which an argument should be resolvedextensionContext - the context in which the current test or container is being
executedparameterContext describes a parameter of type: TemporaryFolder, false otherwiseorg.junit.jupiter.api.extension.ParameterResolutionExceptionpublic Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
supportsParameter(ParameterContext, ExtensionContext) gate.resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolverparameterContext - the context for the parameter for which an argument should be resolvedextensionContext - the context in which the current test or container is being
executedTemporaryFolderorg.junit.jupiter.api.extension.ParameterResolutionExceptionCopyright © 2022. All rights reserved.