public class TestingDir extends Object implements org.junit.rules.TestRule
Rule
to provide a common, easy to use, testing directory that is unique per unit test method.
Similar in scope to the TemporaryFolder
rule, as it creates a directory, when asked (via getDir()
or getEmptyDir()
) in the maven
project familiar and friendly maven location of ${basedir}/target/tests/${testclass}/${testmethod}
.
Note: MavenTestingUtils
will keep the directory name short for the sake of windows users.
This is best suited for Tests that will benefit from a unit directory per test method.
If you desire to have a test directory per test class, with all test methods sharing the same test directory, then
consider using the MavenTestingUtils.getTargetTestingDir(String)
Example use:
public class TestingDirTest { @Rule public TestingDir testingdir = new TestingDir(); @Test public void testUseDirectory() { File appDir = testingdir.getFile("app"); File tmpDir = testingdir.getFile("tmp"); FS.ensureDirEmpty(appDir); FS.ensureDirEmpty(tmpDir); File index = new File(appDir, "index.html"); FileWriter writer = new FileWriter(index); writer.write("Hello World"); writer.close(); Server server = new Server(); server.setTmpDir(tmpDir); server.addWebApp(appDir); server.start(); Client client = new Client(); String response = client.request("http://localhost/app/"); Assert.assertThat(response,containsString("Hello World")); } }
Constructor and Description |
---|
TestingDir() |
Modifier and Type | Method and Description |
---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement statement,
org.junit.runner.Description description) |
void |
ensureEmpty()
Ensure that the test directory is empty.
|
File |
getDir()
Deprecated.
use
javax.nio.file replacement getPath() instead |
File |
getEmptyDir()
Deprecated.
use
javax.nio.file replacement getEmptyPathDir() instead |
Path |
getEmptyPathDir()
Get the unique testing directory while ensuring that it is empty (if not).
|
File |
getFile(String name)
Deprecated.
use
javax.nio.file replacement getPathFile(String) instead |
Path |
getPath()
Get the test specific directory to use for testing work directory.
|
Path |
getPathFile(String name)
Get a
Path file reference for content inside of the test specific test directory. |
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
apply
in interface org.junit.rules.TestRule
public Path getPath()
Name is derived from the test classname & method name.
@Deprecated public File getDir()
javax.nio.file
replacement getPath()
insteadName is derived from the test classname & method name.
@Deprecated public File getFile(String name)
javax.nio.file
replacement getPathFile(String)
insteadNote: No assertions are made if the file exists or not.
name
- the path name of the file (supports deep paths)public Path getPathFile(String name)
Path
file reference for content inside of the test specific test directory.
Note: No assertions are made if the file exists or not.
name
- the path name of the file (supports deep paths)public void ensureEmpty()
Useful for repeated testing without using the maven clean
goal (such as within Eclipse).
@Deprecated public File getEmptyDir()
javax.nio.file
replacement getEmptyPathDir()
insteadpublic Path getEmptyPathDir()
Copyright © 1995–2023 Mort Bay Consulting. All rights reserved.