If you need a temporary
directory or file for testing and you are using jUnit, @Rule together with TemporaryFolder solves your problem.
The TemporaryFolder Rule allows
creation of files and folders that are guaranteed to be deleted when the test
method finishes (whether it passes or fails)
@Rule
public
TemporaryFolder tempFolder = new TemporaryFolder();
...@Test
public void testFileCreation (){
File myFolder = tempFolder.newFile("test.);
fileGenerator.createFiles(2, myFolder);
myFolder.listFiles().length == 2;
}
The files that were generated will be deleted once the test finishes executing.