Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does XMLUnit have an assert to ignore whitespace

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()); } 
like image 337
Scott Avatar asked Apr 19 '11 18:04

Scott


2 Answers

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

XMLUnit.setIgnoreWhitespace(true) 
like image 115
Kai Sternad Avatar answered Sep 24 '22 02:09

Kai Sternad


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(); 
like image 34
Anders Rabo Thorbeck Avatar answered Sep 22 '22 02:09

Anders Rabo Thorbeck