Reputation: 962
I have two XML files and I am comparing them using XUnit using the following test:
@Test
public void testSortXML() throws IOException
{
field = "userid";
outputStream = new ByteArrayOutputStream();
xmlStreamSorter.sort(inputStream, outputStream, comparator);
ByteArrayInputStream expected = new ByteArrayInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <users><user><userid>10</userid></user><user><userid>3</userid></user><user><userid>1</userid></user></users>"
.getBytes());
System.out.println(outputStream);
Assert.assertThat(expected, CompareMatcher.isIdenticalTo(outputStream));
}
But the test fails with the following error:
Expected child nodelist length '3' but was '0' - comparing <users...> at /users[1] to <byteArrayOutputStream...> at /byteArrayOutputStream[1] (DIFFERENT)
This is the content of the output variable:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<userid>10</userid>
</user>
<user>
<userid>3</userid>
</user>
<user>
<userid>1</userid>
</user>
</users>
They are identical. How come the test fails?
Upvotes: 2
Views: 2349
Reputation: 962
The problem was that I did not ignored the whitespace. To do it with XUnit 2.x just create a custom Diff this way:
Diff xmlDiff = DiffBuilder.compare(expected)
.withTest(outputStream.toString())
.ignoreComments()
.ignoreWhitespace()
.build();
And then you can do this:
Assert.assertFalse(
"The resulting XML is not correctly sorted",
xmlDiff.hasDifferences()
);
Upvotes: 4