spock.lang.TempDir Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spock-core Show documentation
Show all versions of spock-core Show documentation
Spock is a testing and specification framework for Java and Groovy applications.
What makes it stand out from the crowd is its beautiful and highly expressive specification language.
Thanks to its JUnit runner, Spock is compatible with most IDEs, build tools, and continuous integration servers.
Spock is inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms.
package spock.lang;
import org.spockframework.runtime.extension.ExtensionAnnotation;
import org.spockframework.runtime.extension.builtin.TempDirExtension;
import org.spockframework.util.Beta;
import spock.util.io.FileSystemFixture;
import java.io.File;
import java.lang.annotation.*;
import java.nio.file.Path;
/**
* Generate a temp directory for test, and delete it after test.
*
*
* {@code @TempDir} can be used to annotate a member field of type {@link File}, {@link Path},
* or untyped like {@code def}/{@link Object} in a spec class (untyped field will be injected with {@code Path}).
*
* Alternatively, you can use it with any class that has a public constructor with a single {@link File} or {@link Path}
* parameter, like {@link FileSystemFixture} this way you can use your own utility classes for file manipulation.
*
* If the annotated field is shared, the temp directory will be shared in this spec,
* otherwise every iteration will have its own temp directory.
*
* Example:
*
* @TempDir
* File testFile // will inject a File
*
* @TempDir
* Path testPath // will inject a Path
*
* @TempDir
* def testPath // will inject a Path
*
* @TempDir
* FileSystemFixture fsFixture // will inject an instance of FileSystemFixture with the temp path injected via constructor
*
*
* @author dqyuan
* @since 2.0
*/
@Beta
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@ExtensionAnnotation(TempDirExtension.class)
public @interface TempDir {
}