2

I am using XMLunit to compare the following XML's

XML 1

<element1>
</element1>
<element2>   Some Text  </element2>

XML 2

<element1/>
<element2>Some Text</element2>

I am expecting only element 2 to come in difference however I get a difference like this for element1

"Expected presence of Child node to be true but was false".
"Expected number of child node 1 but was 0"
"Expected presence of child node '#text' but was null.

I know this can go away with setIgnoreWhiteSpace but I want the element2 whitespace difference.

M A
  • 71,713
  • 13
  • 134
  • 174
Anshul
  • 267
  • 1
  • 4
  • 13
  • Both elements differ in terms of whitespace, effectively - why would you expect element2 to show a difference but not element1? – Jon Skeet Sep 06 '15 at 08:30

2 Answers2

2

<element1/> and <element1></element1> should return the same, but you have a newline between your tags, so that's a 1 or 2 character text node (depending on whether newline is CR LF or just LF).

Since you didn't tell parser to ignore whitespace, it will give your everything it finds in the source data. It is up to you to decide what to do with it.

Andreas
  • 154,647
  • 11
  • 152
  • 247
-1

As others mentioned, XMLUnit treats the text (which consists of a new line) inside <element1> to be a child node. The <element1> in the other XML does not have any child node (not even an empty text).

If you want to ignore such a difference, you'd have to write a custom DifferenceListener:

Diff diff = new Diff(doc1, doc2);
diff.overrideDifferenceListener(new DifferenceListener() {

    ...

    @Override
    public int differenceFound(Difference difference) {
        if(difference.getId() == DifferenceConstants.HAS_CHILD_NODES_ID) {
            Node controlNode = difference.getControlNodeDetail().getNode();
            Node testNode = difference.getTestNodeDetail().getNode();

            if(controlNode.getChildNodes().getLength() == 0) {
                Node childNode = testNode.getChildNodes().item(0);
                // note the trim method call
                if(childNode.getNodeType() == Node.TEXT_NODE
                          && childNode.getTextContent().trim().isEmpty()) {
                    return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
            } else { // we're sure the other node has children in the else part
                Node childNode = controlNode.getChildNodes().item(0);
                if(childNode.getNodeType() == Node.TEXT_NODE
                          && childNode.getTextContent().trim().isEmpty()) {
                    return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
            }
        }
        return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
    }
});
M A
  • 71,713
  • 13
  • 134
  • 174
  • There is no reason for a custom `DifferenceListener` in this case, Just telling XMLUnit to ignore element content whitespace via `XMLUnit.setIgnoreWhitespace(true)` will do. – Stefan Bodewig Sep 07 '15 at 04:14
  • @StefanBodewig This will also ignore the other difference, which the OP does not want. – M A Sep 07 '15 at 06:31
  • you are correct, I misread the requirement - and am now with Jon Skeet. – Stefan Bodewig Sep 08 '15 at 08:13