I want to compare two xml strings in a test, but the test keeps failing due to whitespace.
@Test public void testForEquality() throws Exception { String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>"; String myTestXML = "<msg><uuid>0x00435A8C</uuid> </msg>"; assertXMLEqual(myControlXML, myTestXML); Diff diff = new Diff(myControlXML, myTestXML); assertTrue(diff.similar()); }
Yes, XMLUnit can ignore whitespaces. See API documentation for details. You can enable it by setting:
XMLUnit.setIgnoreWhitespace(true)
The API has changed with XMLUnit 2.x.
Now, for unit tests, you can ignore whitespace with a hamcrest matcher like so:
import static org.hamcrest.MatcherAssert.assertThat; import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo; ... assertThat(actual, isIdenticalTo(expected).ignoreWhitespace());
Or alternatively, with the builder API directly:
import org.xmlunit.builder.DiffBuilder; ... boolean areDifferent = DiffBuilder.compare(left).withTest(right) .ignoreWhitespace().build().hasDifferences();
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