1

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) {}
}
mrcrabs
  • 141
  • 2
  • 7

3 Answers3

2

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"); 
mrcrabs
  • 141
  • 2
  • 7
1

It looks a bit you expected that any whitespace characters is to be ignored but that is just not the case for that setting.

See XMLUnit.setIgnoreWhitespace:

Whether to ignore whitespace when comparing node values.

[...]

Setting this parameter has no effect on whitespace inside texts.

This setting does not mean that whitespace-only text-nodes will be removed (and not checked for). It will only mean that the whitespace is ignored when comparing the node values of the <test> elements. But that test didn't cause the message. In your case a check for the count of child-nodes did raise the message.

See 3.8.1. Whitespace Handling for a more detailed description and more configuration options (e.g. XMLUnit.setNormalizeWhitespace).

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • But is not a text, it contains node? Anyway code below resolved the issue, so pretty sure that was it... – mrcrabs Dec 13 '12 at 05:14
  • No, contains a children that is a textnode (that space). As that textnode is expected, it's looked for. You might want to report this as a bug, too, because according to the docs it must not be expected with the JAXP libs. – hakre Dec 13 '12 at 05:18
-1

Overriding the JAXP libs as @user392909 did, did not succeed for me.

But a conflict with another library in my project was the root cause. First I changed the import order of some libraries which finally fixed the test. So I was able to identify the conflicting library. It was j2ee.jar.

I removed it (and had to replace it by tomcat-catalina.jar, tomcat-juli.jar and javamail.jar to resolve upcoming compilation errors).

The import order doesn't matter any longer since j2ee.jar was removed / replaced and the xml diff is now working as expected.

Peer
  • 1
  • 1