Search code examples
javaxmlxmlunit

Issue with setIgnoreWhitespace in XMLUnit


I'm having trouble getting xmlunit to ignore whitespace in my XML using setIgnoreWhitespace... any idea what could be wrong or what I could check?

JVM: 1.6, XMLUnit 1.3, IDE: JDeveloper 11.1.1.6

For example the below returns "Expected number of child nodes '2' but was '1'". If I take out the extra space it passes.

@Test
public void testExample()  {
    String inputXML = "<test><innertest>data</innertest></test>";
    String expectedResultXml = "<test> <innertest>data</innertest></test>";

    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreComments(true);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

    try {
        assertXMLEqual("Did not match!!", expectedResultXml, inputXML);
    } catch(Exception e) {}
}

Solution

  • I must have some incompatible XML library in my classpath. Overriding the JAXP libs as below resolved the issue (also added xerces and xalan jars).

        XMLUnit.setControlParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
        XMLUnit.setTestParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
        XMLUnit.setSAXParserFactory("org.apache.xerces.jaxp.SAXParserFactoryImpl");
        XMLUnit.setTransformerFactory("org.apache.xalan.processor.TransformerFactoryImpl");