Search code examples
javajunitcomparetext-files

Comparing text files with Junit


I am comparing text files in junit using:

public static void assertReaders(BufferedReader expected,
          BufferedReader actual) throws IOException {
    String line;
    while ((line = expected.readLine()) != null) {
        assertEquals(line, actual.readLine());
    }

    assertNull("Actual had more lines then the expected.", actual.readLine());
    assertNull("Expected had more lines then the actual.", expected.readLine());
}

Is this a good way to compare text files? What is preferred?


Solution

  • junit-addons has nice support for it: FileAssert

    It gives you exceptions like:

    junitx.framework.ComparisonFailure: aa Line [3] expected: [b] but was:[a]