我在一个名为 Test.java 的类中有这个函数,它包括;
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;
}
所以基本上它将所有文件夹作为“../../tmp”路径中的列表返回。我想为它写一个测试,这就是我所做的,但它不起作用:
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
}
}
我收到以下错误;
..\..\Temp
java.nio.file.NoSuchFileException: ..\..\Temp