java - 如何为 DirectoryStream 编写 junit 测试

标签 java spring-boot unit-testing junit4 junit-jupiter

我在一个名为 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

最佳答案

使用时@TempDir您可以不使用某些特定的硬编码路径(例如,片段中的 "../../Temp")。
基本上,临时目录会由 junit 自动为您创建。 .然后在测试中,您可以按照您想要的方式操作它,例如创建文件和目录。
如果您需要获取临时目录路径的值,您可以简单地调用 toString()在用 @TempDir 注释的字段上.
例如,对于您的特定情况,可以编写以下测试:

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使用 默认系统临时文件目录是的这取决于操作系统,通常可以通过环境变量找到。例如,TMPDIR在我的系统上指向 /tmp目录。

关于java - 如何为 DirectoryStream 编写 junit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64444434/

相关文章:

java - Spring Boot gradle多模块应用

javascript - Jasmine 单元测试: Can't trigger change event in custom angular dropdown component

java - 为什么 hamcrest any(Myclass.class) 需要类型转换

java - 为什么@AfterSuit 在没有alwaysRun=true 的情况下不运行?

java - 通过 POST 从 React App 发送数组到 Spring Boot

java - servlet 中的线程局部变量

android - 如何使用 Spring Boot Server 无限期地保持 Android 应用程序登录?

angularjs - Karma + jasmine + angular + ui 路由器 - 使用 state.go 和参数测试解析

java - Int 大小 字节 套接字 AS3

java - 由于初始化失败,SolrCore 'collection1' 不可用。编辑文件并授予权限后