1

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?

3
  • I have trouble believing that this code compiles. ideone.com/rhWwIN. Commented Nov 29, 2017 at 23:24
  • 1
    The class returned by Arrays.asList though also named ArrayList is not to be confused with the usual ArrayList. To be more precise java.util.Arrays$ArrayList is different to java.util.ArrayList. The first is an immutable wrapper around the given array where the second implements what is often called dynamic arrays. Commented Nov 29, 2017 at 23:26
  • 4
    Your problem is that you're comparing a list that contains a String with a list that contains a File. A String and a File will never be equal. Commented Nov 29, 2017 at 23:28

2 Answers 2

4

as mentioned by Dawood ibn Kareem within the comments, it seems like you're comparing two lists that contain different type of objects, i.e one is a List<File> and the other is a List<String> hence why they will never be equal. You'll need to make sure you're either performing the Assert on a List<File> objects against another List<File> objects or a List<String> against another List<String> objects but not different types.

Also, this string:

"path\start.fxml"

has to be changed to this:

"path\\start.fxml"

to escape the literal \

Sign up to request clarification or add additional context in comments.

Comments

-1

I would suggest using specialized libraries for assertions, especially not trivial ones like yours.

Try hamcrest:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

then in your test:

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
....

@Test
public void ListOfTheFiles(){

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

Update:

you need to add a File instead of a String as the first argument though

11 Comments

That presumably is not going to fix the issue, though - assertEquals should still pass if the lists are indeed equal.
Sorry i missed that .. the lists of course need to be of same type
Can I suggest that you either remove the bit about hamcrest, or make it clear that is a recommendation not directly related to the problem? Using hamcrest won't fix OP's problem.
Yeah the hamcrest bit is confusing me. This absolutely is a trivial assertion. hamcrest doesn't simplify anything here, I think
Yes, although Hamcrest is really good, it's kind of an answer to an entirely different question. The correct answer to this question is cleverly hidden on the very last line. I guess if I want to see an answer that says Assert.assertEquals(Arrays.asList(new File("path\\start.fxml")), result);, I'll have to write it myself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.