53

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
jon077
  • 10,303
  • 11
  • 39
  • 37
  • 1
    I'm giving an upvote for the solution you have in the question. Since this is an older post, JUnit add ons is out of date (probably not compatible with JUnit 4), I don't like Apache Utils and given how concise your assertReaders method is I don't see justification for including a whole new library of utilities. Yours worked right off the bat - thanks! – PMorganCA Apr 17 '20 at 18:21

9 Answers9

52

Here's one simple approach for checking if the files are exactly the same:

assertEquals("The files differ!", 
    FileUtils.readFileToString(file1, "utf-8"), 
    FileUtils.readFileToString(file2, "utf-8"));

Where file1 and file2 are File instances, and FileUtils is from Apache Commons IO.

Not much own code for you to maintain, which is always a plus. :) And very easy if you already happen to use Apache Commons in your project. But no nice, detailed error messages like in mark's solution.

Edit:
Heh, looking closer at the FileUtils API, there's an even simpler way:

assertTrue("The files differ!", FileUtils.contentEquals(file1, file2));

As a bonus, this version works for all files, not just text.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • 4
    The assertTrue form is concise, but relatively useless when it fails. At least the assertEquals method will show you a few characters where they are different – Stephen Jun 10 '10 at 10:33
  • 3
    **Update**: Nowadays I'd recommend [Google Guava](http://code.google.com/p/guava-libraries/) over Commons IO for reading the files as string: `Files.toString(file1, Charset.forName("UTF-8"));` There isn't much difference in a case like this, but overall Guava is a cleaner, better documented and actively maintained library. – Jonik Jul 18 '11 at 18:12
  • 6
    And since **Java 7** you can [read a text file as string](http://stackoverflow.com/a/326440/56285) quite simply without any external libs: `new String(Files.readAllBytes(Paths.get("/path/to/file.txt")), StandardCharsets.UTF_8)` – Jonik Dec 24 '15 at 01:31
31

junit-addons has nice support for it: FileAssert

It gives you exceptions like:

junitx.framework.ComparisonFailure: aa Line [3] expected: [b] but was:[a]
Uooo
  • 6,204
  • 8
  • 36
  • 63
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • 3
    Just a note the latest version vin maven central repo is from 2003 and version 1.4 so i don't know if its compatible with the latest versions. – kohlerfc Nov 19 '15 at 21:00
27

Here is a more exhaustive list of File comparator's in various 3rd-party Java libraries:

Bogdan Calmac
  • 7,993
  • 6
  • 51
  • 64
Scott Langley
  • 374
  • 4
  • 3
21

As of 2015, I would recomment AssertJ, an elegant and comprehensive assertion library. For files, you can assert against another file:

@Test
public void file() {
    File actualFile = new File("actual.txt");
    File expectedFile = new File("expected.txt");
    assertThat(actualFile).hasSameTextualContentAs(expectedFile);
}

or against inline strings:

@Test
public void inline() {
    File actualFile = new File("actual.txt");
    assertThat(linesOf(actualFile)).containsExactly(
            "foo 1",
            "foo 2",
            "foo 3"
    );
}

The failure messages are very informative as well. If a line is different, you get:

java.lang.AssertionError: 
File:
  <actual.txt>
and file:
  <expected.txt>
do not have equal content:
line:<2>, 
Expected :foo 2
Actual   :foo 20

and if one of the files has more lines you get:

java.lang.AssertionError:
File:
  <actual.txt>
and file:
  <expected.txt>
do not have equal content:
line:<4>,
Expected :EOF
Actual   :foo 4
anothernode
  • 5,100
  • 13
  • 43
  • 62
Bogdan Calmac
  • 7,993
  • 6
  • 51
  • 64
  • 2
    The method `hasContentEqualTo` is deprecated as of this comment. Use `hasSameContentAs` instead. – Stephan Feb 01 '17 at 00:44
11

Simple comparison of the content of two files with java.nio.file API.

byte[] file1Bytes = Files.readAllBytes(Paths.get("Path to File 1"));
byte[] file2Bytes = Files.readAllBytes(Paths.get("Path to File 2"));

String file1 = new String(file1Bytes, StandardCharsets.UTF_8);
String file2 = new String(file2Bytes, StandardCharsets.UTF_8);

assertEquals("The content in the strings should match", file1, file2);

Or if you want to compare individual lines:

List<String> file1 = Files.readAllLines(Paths.get("Path to File 1"));
List<String> file2 = Files.readAllLines(Paths.get("Path to File 2"));

assertEquals(file1.size(), file2.size());

for(int i = 0; i < file1.size(); i++) {
   System.out.println("Comparing line: " + i)
   assertEquals(file1.get(i), file2.get(i));
}
Philzen
  • 3,945
  • 30
  • 46
Jonathan Andersson
  • 1,342
  • 3
  • 16
  • 31
7

I'd suggest using Assert.assertThat and a hamcrest matcher (junit 4.5 or later - perhaps even 4.4).

I'd end up with something like:

assertThat(fileUnderTest, containsExactText(expectedFile));

where my matcher is:

class FileMatcher {
   static Matcher<File> containsExactText(File expectedFile){
      return new TypeSafeMatcher<File>(){
         String failure;
         public boolean matchesSafely(File underTest){
            //create readers for each/convert to strings
            //Your implementation here, something like:
              String line;
              while ((line = expected.readLine()) != null) {
                 Matcher<?> equalsMatcher = CoreMatchers.equalTo(line);
                 String actualLine = actual.readLine();
                 if (!equalsMatcher.matches(actualLine){
                    failure = equalsMatcher.describeFailure(actualLine);
                    return false;
                 }
              }
              //record failures for uneven lines
         }

         public String describeFailure(File underTest);
             return failure;
         }
      }
   }
}

Matcher pros:

  • Composition and reuse
  • Use in normal code as well as test
    • Collections
    • Used in mock framework(s)
    • Can be used a general predicate function
  • Really nice log-ability
  • Can be combined with other matchers and descriptions and failure descriptions are accurate and precise

Cons:

  • Well it's pretty obvious right? This is way more verbose than assert or junitx (for this particular case)
  • You'll probably need to include the hamcrest libs to get the most benefit
Stephen
  • 19,488
  • 10
  • 62
  • 83
5

FileUtils sure is a good one. Here's yet another simple approach for checking if the files are exactly the same.

assertEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));

While the assertEquals() does provide a little more feedback than the assertTrue(), the result of checksumCRC32() is a long. So, that may not be intrisically helpful.

Mookie Wilson
  • 188
  • 2
  • 10
  • 1
    +1, I guess this could come in handy for really large files (when you only care about whether the files differ, not what the difference is) – Jonik Jul 26 '11 at 16:52
4

If expected has more lines than actual, you'll fail an assertEquals before getting to the assertNull later.

It's fairly easy to fix though:

public static void assertReaders(BufferedReader expected,
    BufferedReader actual) throws IOException {
  String expectedLine;
  while ((expectedLine = expected.readLine()) != null) {
    String actualLine = actual.readLine();
    assertNotNull("Expected had more lines then the actual.", actualLine);
    assertEquals(expectedLine, actualLine);
  }
  assertNull("Actual had more lines then the expected.", actual.readLine());
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I like that your answer doesn't rely on any third party libraries but this code won't compile. The scope of the variable 'actual' is limited to the while-loop so the final assertNull line won't compile. – buzz3791 Jan 31 '14 at 20:30
  • @buzz3791: No, the scope of `actualLine` is limited to the while loop. The scope of `actual` is the whole method. – Jon Skeet Jan 31 '14 at 20:32
0

This is my own implementation of equalFiles, no need to add any library to your project.

private static boolean equalFiles(String expectedFileName,
        String resultFileName) {
    boolean equal;
    BufferedReader bExp;
    BufferedReader bRes;
    String expLine ;
    String resLine ;

    equal = false;
    bExp = null ;
    bRes = null ;

    try {
        bExp = new BufferedReader(new FileReader(expectedFileName));
        bRes = new BufferedReader(new FileReader(resultFileName));

        if ((bExp != null) && (bRes != null)) {
            expLine = bExp.readLine() ;
            resLine = bRes.readLine() ;

            equal = ((expLine == null) && (resLine == null)) || ((expLine != null) && expLine.equals(resLine)) ;

            while(equal && expLine != null)
            {
                expLine = bExp.readLine() ;
                resLine = bRes.readLine() ; 
                equal = expLine.equals(resLine) ;
            }
        }
    } catch (Exception e) {

    } finally {
        try {
            if (bExp != null) {
                bExp.close();
            }
            if (bRes != null) {
                bRes.close();
            }
        } catch (Exception e) {
        }

    }

    return equal;

}

And to use it just use regular AssertTrue JUnit method

assertTrue(equalFiles(expected, output)) ;
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99