gpt4 book ai didi

Java - JUnit 测试 - ArrayList

转载 作者:行者123 更新时间:2023-11-29 04:23:44 27 4
gpt4 key购买 nike

我写了一个类来查找进入目录和子目录的文件:

public class DirectoryWalker {

public List<File> walk(String path) {

File root = new File(path);
List<File> resultList = new ArrayList<>();
File[] list = root.listFiles();
resultList.addAll(Arrays.asList(list));
if (list == null) return null;

for (File f : list) {
if (f.isDirectory()) {
walk(f.getAbsolutePath());
System.out.println("Dir:" + f.getAbsoluteFile());
} else {
System.out.println("File:" + f.getAbsoluteFile());
}
}
return resultList;
}
}

现在我正在尝试使用 JUnit 进行测试:

@Test
public void ListOfTheFiles(){

List<File> result = directoryWalker.walk("path");
Assert.assertEquals(Arrays.asList("path\start.fxml"), result);

测试提示:

    Expected :java.util.Arrays$ArrayList<[path\start.fxml]> 
Actual :java.util.ArrayList<[path\start.fxml]>

在这种情况下如何正确测试 ArrayList?

最佳答案

正如 Dawood ibn Kareem 在评论中提到的,您似乎正在比较两个包含不同类型对象的列表,即一个是 List<File>另一个是 List<String>因此,为什么他们永远不会平等。您需要确保您正在执行 AssertList<File> 上反对另一个对象List<File>对象或 List<String>反对另一个List<String>对象,但不是不同的类型。

另外,这个字符串:

"path\start.fxml"

必须改成这样:

"path\\start.fxml"

转义文字 \

关于Java - JUnit 测试 - ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47563485/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com