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 getPath() or getEmptyPathDir()) 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() throws IOException
{
Path appDir = testingdir.getPathFile("app");
Path tmpDir = testingdir.getPathFile("tmp");
FS.ensureEmpty(appDir);
FS.ensureEmpty(tmpDir);
Path index = appDir.resolve("index.html");
try(BufferedWriter writer = Files.newBufferedWriter(index, StandardCharsets.UTF_8))
{
writer.write("Hello World");
}
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.
|
Path |
getEmptyPathDir()
Get the unique testing directory while ensuring that it is empty (if not).
|
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.TestRulepublic Path getPath()
Name is derived from the test classname & method name.
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).
public Path getEmptyPathDir()
Copyright © 1995–2017 Mort Bay Consulting. All rights reserved.