英文:
How to write junit test for DirectoryStream
问题
public List
final List
try (DirectoryStream
for (Path entry : stream) {
if (Files.isDirectory(entry)) {
files.add(entry);
}
}
}
return files;
}
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
}
}
java.nio.file.NoSuchFileException: ....\Temp
英文:
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
答案1
得分: 1
当使用@TempDir
时,您可以自由地不使用一些特定的硬编码路径(例如代码片段中的"../../Temp"
)。
基本上,临时目录将由junit
自动为您创建。然后在测试中,您可以按照您想要的方式进行操作,例如创建文件和目录。
如果您需要获取临时目录路径的值,只需在用@TempDir
注释的字段上调用toString()
。
例如,针对您的特定情况,以下测试可以编写:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class FoldersResolverTest {
@TempDir
Path directory;
// 假设 FoldersResolver 包装了 getAllFoldersInTmp
FoldersResolver foldersResolver = new FoldersResolver();
@Test
void directoriesAreFoundAndFilesAreSkipped() throws IOException {
Path fileOne = directory.resolve("file1");
Path fileTwo = directory.resolve("file2");
Path directoryOne = directory.resolve("directory1");
Path directoryTwo = directory.resolve("directory2");
Files.createFile(fileOne);
Files.createFile(fileTwo);
Files.createDirectory(directoryOne);
Files.createDirectory(directoryTwo);
// 请注意,directory.toString() 返回由 junit 创建的临时文件夹的路径
List<Path> actual = foldersResolver.getAllFoldersInTmp(directory.toString());
List<Path> expected = List.of(directoryOne.getFileName(), directoryTwo.getFileName());
assertEquals(expected, actual);
}
}
注意: 通常情况下,@TempDir
使用默认的系统临时文件目录。这取决于操作系统,并且通常可以通过环境变量找到。例如,我的系统上TMPDIR
指向/tmp
目录。
英文:
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:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class FoldersResolverTest {
@TempDir
Path directory;
// assume FoldersResolver wrapps getAllFoldersInTmp
FoldersResolver foldersResolver = new FoldersResolver();
@Test
void directoriesAreFoundAndFilesAreSkipped() throws IOException {
Path fileOne = directory.resolve("file1");
Path fileTwo = directory.resolve("file2");
Path directoryOne = directory.resolve("directory1");
Path directoryTwo = directory.resolve("directory2");
Files.createFile(fileOne);
Files.createFile(fileTwo);
Files.createDirectory(directoryOne);
Files.createDirectory(directoryTwo);
// note directory.toString() returns path to the temporary folder created by junit
List<Path> actual = foldersResolver.getAllFoldersInTmp(directory.toString());
List<Path> expected = List.of(directoryOne.getFileName(), directoryTwo.getFileName());
assertEquals(expected, actual);
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论