question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create example for TemporaryFolder extension

See original GitHub issue

What 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:closed
  • Created 8 years ago
  • Comments:31 (23 by maintainers)

github_iconTop GitHub Comments

1reaction
mniederocommented, Apr 27, 2018

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.

0reactions
sormurascommented, May 16, 2018

It only took you (now: us) two years and a couple of month!

🥂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found