Create example for TemporaryFolder extension
See original GitHub issueWhat would an implementation outline look like?
Usage:
@ExtendWith(TemporaryFolder.class)
public class TempTest {
// ...
@Test
void temp(@TemporaryFolder.Tag TemporaryFolder temp) throws IOException {
System.out.println(temp.getRoot());
}
}
Implementation (“copied” from JUnit 4, stripped ExternalResource dependency):
public class TemporaryFolder implements MethodParameterResolver, AfterAllExtensionPoint {
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tag { }
// ...
@Override
public Object resolve(Parameter parameter, MethodInvocationContext mci, ExtensionContext ec)
throws ParameterResolutionException {
try {
create();
} catch (IOException e) {
throw new ParameterResolutionException(e.toString());
}
return this;
}
@Override
public boolean supports(Parameter parameter, MethodInvocationContext mci, ExtensionContext ec)
throws ParameterResolutionException {
return parameter.isAnnotationPresent(Tag.class);
}
@Override
public void afterAll(ContainerExtensionContext arg0) throws Exception {
delete();
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:31 (23 by maintainers)
Top Results From Across the Web
TemporaryFolderExtension - JUnit-Extensions Documentation
This annotation results in a TemporaryFolder instance being injected into the test case or test method. You can then invoke methods on TemporaryFolder...
Read more >JUnit 5 Temporary Directory Support - Baeldung
As we'll see later, we can use this extension to create and clean up a temporary directory for an individual test or all...
Read more >TemporaryFolder (JUnit API)
Object extended by ... The TemporaryFolder Rule allows creation of files and folders that should be ... Override to set up your specific...
Read more >Migrating from JUnit 4 to JUnit 5: replacing rules with the ...
Migrating from JUnit 4 to JUnit 5: replacing rules with the extension model. Part 2 ... We use the TemporaryFolder field to create...
Read more >JUnit TemporaryFolder Rule Example - Java Articles
TemporaryFolder rule is an extension of ExternalResource rule. It is used to create a new fresh folder with a random name under the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Indeed, it would be great if there is a replacement of JUnit 4 TemporaryFolder within JUnit 5.
Here, I’ve re-engineered lot of JUnit tests for following reasons: o) make them platform independent (Win/Linux). We develop on Windows but Jenkins CI is running on Linux (RHEL 7 like) o) have a testbed that is celeaned-up/destroyed automatically after success. This is essential on Jenkins CI because I’m interesseted to minimize time needed for maintenance as much as possible. o) lot of my tests are like integration test: They read and write files. But, writing files into folder structure containing source code is strictly forbidden.
Well, I can write my own TemporaryFolder code like ten years (or more) ago. Since a Temporaryfolder rule is available with JUnit 4 this is no longer feasible. For the moment, missing TemporaryFolder Extension is biggest hurdle for migration from Junit 4 to JUnit 5. No, it has become a no-go.
It only took you (now: us) two years and a couple of month!
🥂