This project provides a JUnit Jupiter extension for in-memory
@TempDir
directories via the Jimfs file system.
Today it is already possible to use Jimfs and JUnit Jupiter together to create in-memory temporary directories for testing. However, it requires Jimfs in-memory file system handling hooked into JUnit Jupiter test lifecycle callbacks, a boilerplate that users must implement on their own.
Starting from version 5.10,
JUnit Jupiter offers a
TempDirFactory
SPI
for customizing how temporary directories are created via the @TempDir
annotation.
The SPI allows libraries like Jimfs to provide their own implementation.
I asked Google to provide first-party support in google/jimfs#258. However, Google has not yet started using JUnit Jupiter and first-party support may only be provided when Google does so.
For this reason, I decided to implement this extension to aid all the users that would like a smooth integration between Jimfs and JUnit Jupiter. If Google ever offers first-party support for this integration, this project will likely be discontinued.
Jimfs JUnit Jupiter is based on JUnit Jupiter 5, thus requiring at least Java 8.
Compatibility is guaranteed only with the JUnit Jupiter versions from 5.10 to the latest.
<dependency>
<groupId>io.github.scordio</groupId>
<artifactId>jimfs-junit-jupiter</artifactId>
<version>${jimfs-junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
testImplementation("io.github.scordio:jimfs-junit-jupiter:${jimfsJunitJupiterVersion}")
The simplest possible usage is to set the
factory
attribute of @TempDir
to JimfsTempDirFactory
:
@Test
void test(@TempDir(factory = JimfsTempDirFactory.class) Path tempDir) {
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs");
}
tempDir
is resolved into an in-memory temporary directory based on Jimfs, appropriately configured for the current
operating system.
@JimfsTempDir
, a @TempDir
composed annotation,
can be used as a drop-in replacement for
@TempDir(factory = JimfsTempDirFactory.class)
:
@Test
void test(@JimfsTempDir Path tempDir) {
assertThat(tempDir.getFileSystem().provider().getScheme()).isEqualTo("jimfs");
}
The default behavior of the annotation is equivalent to using JimfsTempDirFactory
directly:
tempDir
is resolved into an in-memory temporary directory based on Jimfs, appropriately configured for the current
operating system.
For better control over the underlying in-memory file system,
@JimfsTempDir
offers an optional value
attribute that can be set to the desired configuration, one of:
FOR_CURRENT_PLATFORM
: appropriate to the current operating system (default)OS_X
: for a Mac OS X-like file systemUNIX
: for a UNIX-like file systemWINDOWS
: for a Windows-like file system
For example, the following defines a Windows-like temporary directory regardless of the operating system the test is running on:
@Test
void test(@JimfsTempDir(WINDOWS) Path tempDir) {
assertThat(tempDir.getFileSystem().getSeparator()).isEqualTo("\\");
}
The junit.jupiter.tempdir.factory.default
configuration parameter
can be used to specify the fully qualified class name of the factory to be used by default.
For example, the following configures JimfsTempDirFactory
:
junit.jupiter.tempdir.factory.default=io.github.scordio.jimfs.junit.jupiter.JimfsTempDirFactory
The factory will be used for all @TempDir
annotations unless the factory
attribute of the annotation
specifies a different type.
Jimfs JUnit Jupiter only supports annotated fields or parameters of type Path
, as Jimfs is a non-default file
system and File
instances can only be associated with the default file system.
Compared to the configuration options that Jimfs provides, Jimfs JUnit Jupiter exposes a much smaller surface to keep its usage simple.
In case something is missing for your use case, please raise an issue!
Jimfs JUnit Jupiter is released under version 2.0 of the Apache License.