Does XMLUnit have an assert to ignore whitespace
Asked Answered
M

2

32

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());
}
Meander answered 19/4, 2011 at 18:59 Comment(1)
Guess I should have looked for 5 more minutes XMLUnit.setIgnoreWhitespace(true);Meander
V
44

Yes, XMLUnit can ignore whitespaces. See API documentation for details. You can enable it by setting:

XMLUnit.setIgnoreWhitespace(true)
Verified answered 19/4, 2011 at 19:7 Comment(0)
K
14

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();
Kc answered 8/6, 2017 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.