Friday, March 20, 2009

XMLUnit for xml assertions

Thought i'd blog this little snippet. Was struggling with xml comparisons for unit testing, I initially tried XMLUnit but found that it was too sensitive. After a bit of digging in the docs, I found how to configure it to be much more forgiving on element ordering, whitespace etc. Here is what I did, hopefully others will find this useful:

protected void setUp() throws Exception {
XMLUnit.setIgnoreComments( true );
XMLUnit.setIgnoreWhitespace( true );
XMLUnit.setIgnoreAttributeOrder( true );
XMLUnit.setNormalizeWhitespace( true );
XMLUnit.setNormalize( true );

private void assertXMLEqual(String expectedXml,
String resultXml) {
try {
Diff diff = new Diff( expectedXml,
resultXml );
diff.overrideElementQualifier( new RecursiveElementNameAndTextQualifier() );
XMLAssert.assertXMLEqual( diff,
true );
} catch ( Exception e ) {
throw new RuntimeException( "XML Assertion failure",
e );


1 comment:

  1. Hi, I work for an IT company in Perú. We are using Drools to develop an expert system to set product pricing. But I'm facing a problem using Drools, maybe bcause I'm new using it. I posted my problem here:

    Please, any help with this would be very appreciated. It's very urgent.