如何编写针对DirectoryStream的JUnit测试

huangapple go评论61阅读模式
英文:

How to write junit test for DirectoryStream

问题

public List getAllFoldersInTmp(String directory) throws IOException {
final List files = new ArrayList<>();
try (DirectoryStream stream = Files.newDirectoryStream(Path.of(directory))) {
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&lt;Path&gt; getAllFoldersInTmp(String directory) throws IOException {
    final List&lt;Path&gt; files = new ArrayList&lt;&gt;();
    try (DirectoryStream&lt;Path&gt; 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(&quot;file1&quot;).toFile();
        File fileTwo = directory.resolve(&quot;file2&quot;).toFile();
    
        System.out.println(test.getAllFoldersInTmp(&quot;../../Temp&quot;)); //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时,您可以自由地不使用一些特定的硬编码路径(例如代码片段中的&quot;../../Temp&quot;)。

基本上,临时目录将由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. &quot;../../Temp&quot; 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(&quot;file1&quot;);
        Path fileTwo = directory.resolve(&quot;file2&quot;);
        Path directoryOne = directory.resolve(&quot;directory1&quot;);
        Path directoryTwo = directory.resolve(&quot;directory2&quot;);

        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&lt;Path&gt; actual = foldersResolver.getAllFoldersInTmp(directory.toString());

        List&lt;Path&gt; 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.

huangapple
  • 本文由 发表于 2020年10月20日 19:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64444434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定