public class TemporaryFolderExtension extends Object implements org.junit.jupiter.api.extension.AfterEachCallback, 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 either of the following approaches:
@BeforeEach method. For example:
private TemporaryFolder temporaryFolder;
@BeforeEach
public void setUp(TemporaryFolder temporaryFolder) {
this.temporaryFolder = temporaryFolder
// ...
}
@Test method. For example:
@Test
public void testUsingTemporaryFolder(TemporaryFolder temporaryFolder) {
// ...
}
In both approaches the TemporaryFolder will be destroyed during @AfterEach and
no exception will be thrown in cases where the deletion fails.
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() {
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) {
File file = temporaryFolder.createDirectory("foo");
// ...
}
}
| Constructor and Description |
|---|
TemporaryFolderExtension() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
If there is a
TemporaryFolder associated with the current extensionContext then
destroy it. |
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 void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
TemporaryFolder associated with the current extensionContext then
destroy it.afterEach in interface org.junit.jupiter.api.extension.AfterEachCallbackextensionContext - the context in which the current test or container is being
executedpublic 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 © 2018. All rights reserved.