I want to create a Unit test method. Version of Java - 1.6
@Test
public void TestCreateHtml() throws IOException{
final File output = parser.createHtml();
final File expected = new File("src/main/resources/head.jsp");
assertEquals("The files differ!", FileUtils.readLines(expected), FileUtils.readLines(output));
}
This test method doesn't work. The contents of both files are equals, but they have different number of white spaces.
How can I ignore the white spaces?
If the problem is in leading/trailing white space:
assertEquals(actual.trim(), expected.trim());
If problem is in file contents, only solution I can think of is to remove all white space from both inputs:
assertEquals(removeWhiteSpaces(actual), removeWhiteSpaces(expected));
where removeWhiteSpaces() method looks like this:
String removeWhiteSpaces(String input) {
return input.replaceAll("\\s+", "");
}
If the problem is only leading/trailing white spaces, you can compare line by line after trimming both. This does not work if there can also be extra newlines in one file compared to the other.
@Test
public void TestCreateHtml() throws IOException{
final File output = parser.createHtml();
final File expected = new File("src/main/resources/head.jsp");
List<String> a = FileUtils.readLines(expected);
List<String> b = FileUtils.readLines(output);
assertEquals("The files differ!", a.size(), b.size());
for(int i = 0; i < a.size(); ++i)
assertEquals("The files differ!", a.get(i).trim(), b.get(i).trim());
}
Iterate over list and trim each line
List<String> result = new ArrayList<String>();
for(String s: FileUtils.readLines(expected)) {
result.add(s.trim());
}
Same with other file.
And then compare new lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With