0

I have a piece of code that generates a json file. I am writing a unit test to compare the contents of this file against expected output. I use the code snippet

Assert.assertEquals(FileUtils.readLines(outputFile), 
                    FileUtils.readLines(new File("expected.json")));

Now, the json generator writes json file using its own formatting, so I just want to compare the contents of two files and trim all spaces,newlines from both while comparing. What is the best way to do this without reading line by line?

Noobie93
  • 59
  • 1
  • 9

1 Answers1

0

The hamcrest package has a matcher class which will do approximately what you want: IsEqualIgnoringWhiteSpace. It squashes spaces and strips most space from the front and back of each line.

You will likely have to normalize the file under test, but the matcher does the heavy lifting for you.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42