java newdirectorystream,如何为DirectoryStream编写JUnit测试

I have this function in a class called Test.java and it incudes;

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;

}

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

解决方案

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

List 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值