Skip to content

guaardvark/mktmp

 
 

Repository files navigation

Temp Directory in JUnit5 Tests, in the target/

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

mvn PDD status Maven Central Javadoc codecov Hits-of-Code License

Very often, in JUnit5 tests you need a temporary directory, which you can check after the tests fail. The standard @TempDir doesn't provide such a possibility, because it deletes the directory when Maven build is over. The annotation in this tiny package solves exactly this problem: it places temporary files in the target/ directory, letting you inspect them after the tests finish.

First, you add this to your pom.xml:

<dependency>
  <groupId>com.yegor256</groupId>
  <artifactId>mktmp</artifactId>
  <version>0.1.0</version>
</dependency>

Then, you use it like this, in your JUnit5 test:

import com.yegor256.Mktmp;
import com.yegor256.MktmpResolver;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(MktmpResolver.class)
class FooTest {
  @Test
  void worksFine(@Mktmp Path tmp) {
    // The "tmp" directory is a subdirectory of
    // the "target/mktmp/" directory, where all
    // temporary directories of all tests will
    // be kept, in order to help you review the
    // leftovers after failed (or passed) tests.
  }
}

You can also annotate a Path or File field with @Mktmp; the extension will inject it before each test instance runs.

How to Contribute

Fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch shortly, provided they don't violate our quality standards. To avoid frustration, before sending us your pull request please run full Maven build:

mvn clean install -Pqulice

You will need Maven 3.3+ and Java 11+.

About

Create JUnit 5 temporary directories under target/ instead of the system temp directory

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Java 100.0%