Class TestingDir
- All Implemented Interfaces:
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"));
}
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statementapply(org.junit.runners.model.Statement statement, org.junit.runner.Description description) voidEnsure that the test directory is empty.Get the unique testing directory while ensuring that it is empty (if not).getPath()Get the test specific directory to use for testing work directory.getPathFile(String name) Get aPathfile reference for content inside of the test specific test directory.
-
Field Details
-
dir
-
-
Constructor Details
-
TestingDir
public TestingDir()
-
-
Method Details
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description) - Specified by:
applyin interfaceorg.junit.rules.TestRule
-
getPath
Get the test specific directory to use for testing work directory.Name is derived from the test classname & method name.
- Returns:
- the test specific directory.
-
getPathFile
-
ensureEmpty
public void ensureEmpty()Ensure that the test directory is empty.Useful for repeated testing without using the maven
cleangoal (such as within Eclipse). -
getEmptyPathDir
Get the unique testing directory while ensuring that it is empty (if not).- Returns:
- the unique testing directory, created, and empty.
-