public class DirTestWatcher
extends org.junit.rules.TestWatcher
This class is used to create consistently named and safe temp directories for unit tests.
A DirTestWatcher is added to a test by declaring it as a JUnit
Rule. A Rule is a piece of code that
is run before and after every JUnit test marked with the
Test annotation. When the DirTestWatcher is
added to a test class the DirTestWatcher will create a temp directory
before each of your Tests and optionally delete the
temp directory after each of your Tests. The base
temp directory created by the DirTestWatcher is in the
target folder of the maven project and has the form (my test class
fully qualified name)/(my test method name). So in the context of the
code example below, the temp directory created for each test in target
will be my.proj.MyTestClass/myTestMethod1 and
my.proj.MyTestClass/myTestMethod2 respectively.
The temp directory created by the DirTestWatcher can be used within a
test by simply calling the getDir() method on the
DirTestWatcher within your unit test.
By default, the DirTestWatcher deletes the temp directory it creates
at the end of each Test. However, you can create a
DirTestWatcher by doing new DirTestWatcher(false) to disable
the deletion of temp directories after a test. This is useful if you want to
examine files after a test runs.
package my.proj;
public class MyTestClass {
@Rule
public final DirTestWatcher dirTestWatcher = new DirTestWatcher();
@Test
public void myTestMethod1() {
File dir = dirTestWatcher.getDir();
// Do stuff in the temp directory
}
@Test
public void myTestMethod2() {
File dir = dirTestWatcher.getDir();
// Do stuff in the temp directory
}
}
Note: In the code sample above, the directories returned by
getDir() in myTestMethod1 and myTestMethod2 are
my.proj.MyTestClass/myTestMethod1 and
my.proj.MyTestClass/myTestMethod2 respectively.
| Constructor and Description |
|---|
DirTestWatcher()
Creates a
DirTestWatcher that deletes temp directories after the
TestWatcher completes. |
DirTestWatcher(boolean deleteDirAtEnd)
Creates a
DirTestWatcher which can delete or keep the temp
directory after the TestWatcher completes. |
| Modifier and Type | Method and Description |
|---|---|
static File |
createTempDir(File baseDir) |
protected void |
failed(Throwable e,
org.junit.runner.Description description) |
protected void |
finished(org.junit.runner.Description description) |
File |
getDir()
Gets the
File object of the current temp directory. |
File |
makeSubDir(Path relativeDirPath)
Creates a sub directory with the given relative path in current temp
directory
|
protected void |
starting(org.junit.runner.Description description) |
public DirTestWatcher()
DirTestWatcher that deletes temp directories after the
TestWatcher completes.public DirTestWatcher(boolean deleteDirAtEnd)
DirTestWatcher which can delete or keep the temp
directory after the TestWatcher completes.deleteDirAtEnd - When true the temp directory created by the DirTestWatcher
is deleted. When false the temp directory created by the
DirTestWatcher is not deleted.protected void starting(org.junit.runner.Description description)
starting in class org.junit.rules.TestWatcherprotected void finished(org.junit.runner.Description description)
finished in class org.junit.rules.TestWatcherprotected void failed(Throwable e, org.junit.runner.Description description)
failed in class org.junit.rules.TestWatcherpublic File makeSubDir(Path relativeDirPath)
relativeDirPath - The relative path of the sub directory to create in the current
temp directory.File object of the newly created sub directory.public File getDir()
File object of the current temp directory.File object of the current temp directory.Copyright © 2021 The Apache Software Foundation. All rights reserved.