I have this function in a class called Test.java and it incudes;
public List<Path> getAllFoldersInTmp(String directory) throws IOException {
final List<Path> files = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Path.of(directory))) {
for (Path entry : stream) {
if (Files.isDirectory(entry)) {
files.add(entry);
}
}
}
return files;
}
so basically it returns all folders as a list in the "../../tmp" path. I want to write a test for it, and this is how i done but it does not work:
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Testing{
@Autowired
Test test;
@TempDir
Path directory;
@Test
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
File fileOne = directory.resolve("file1").toFile();
File fileTwo = directory.resolve("file2").toFile();
System.out.println(test.getAllFoldersInTmp("../../Temp")); //since the fileone and fileTwo are stored in `Temp/someRandomNumber` directory
}
}
I am getting the following error;
..\..\Temp
java.nio.file.NoSuchFileException: ..\..\Temp
When using
@TempDir
you are free to not use some specific hard-coded paths (e.g."../../Temp"
in the snippent).Basically, the temporary directory will be created for you automatically by
junit
. Then in the tests, you can manipulate it in the way you want, e.g. create files and directories.If you need to get value of temporary directory path, you can simply invoke
toString()
on the field annotated with@TempDir
.For example, for your specific case following test can be written:
Note: In general,
@TempDir
makes use of the default system temporary file directory. This depends on operating system, and normally can be found via environment variable. For example,TMPDIR
on my system points to/tmp
directory.