如何为DirectoryStream编写JUnit测试 [英] How to write junit test for DirectoryStream
问题描述
我在一个名为Test.java的类中具有此功能,并且会导致这种情况;
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;
}
因此,基本上,它会将所有文件夹作为"../../tmp"目录中的列表返回.小路.我想为此编写一个测试,这是我的工作方式,但不起作用:
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
}
}
我遇到以下错误;
..\..\Temp
java.nio.file.NoSuchFileException: ..\..\Temp
推荐答案
使用@TempDir
时,您可以随意不使用某些特定的硬编码路径(例如,片段中的"../../Temp"
).
When using @TempDir
you are free to not use some specific hard-coded paths (e.g. "../../Temp"
in the snippent).
基本上,junit
将自动为您创建临时目录.然后在测试中,您可以按照自己想要的方式进行操作,例如创建文件和目录.
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.
如果需要获取临时目录路径的值,则只需在带有@TempDir
注释的字段上调用toString()
.
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);
}
}
注意:通常,@TempDir
使用默认系统临时文件导演 y.这取决于操作系统,通常可以通过环境变量找到.例如,我系统上的TMPDIR
指向/tmp
目录.
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.
这篇关于如何为DirectoryStream编写JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!