I wrote a class to find the files walking into directory and subdirectory:
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;
}
}
Now I am trying to do the test using JUnit:
@Test
public void ListOfTheFiles(){
List<File> result = directoryWalker.walk("path");
Assert.assertEquals(Arrays.asList("path\start.fxml"), result);
The test complains that:
Expected :java.util.Arrays$ArrayList<[path\start.fxml]>
Actual :java.util.ArrayList<[path\start.fxml]>
How I can test correctly the ArrayList in this case?
Arrays.asListthough also namedArrayListis not to be confused with the usualArrayList. To be more precisejava.util.Arrays$ArrayListis different tojava.util.ArrayList. The first is an immutable wrapper around the given array where the second implements what is often called dynamic arrays.Stringwith a list that contains aFile. AStringand aFilewill never be equal.