3

I wanted to skip two attributes from my acceptance test.So I added following part to remove that two attributes.This is given me a error: junit.framework.AssertionFailedError: org.custommonkey.xmlunit.Diff [different] Expected number of element attributes '2' but was '1'

Part of XML file:

<a:content schemaVS="1"
a:schemaLocate="http://www.ContentXML.xsd"
whiteSpaceMode="preserve">
<section type="Chapter" id="drd121">
    <p type="H1">This is H1.</p>
</section>

Part of Java implementation:

public Document removeIgnoredCxmlNodes(Document resultDocument) {

Element contentElement=(Element) resultDocument.getElementsByTagName("a:content").item(0);
contentElement.removeAttribute("schemaVS");
contentElement.removeAttribute("a:schemaLocate");
return resultDocument;

}

public void cxmlShouldBeProduced(String location) throws Throwable {
try {
    Document expectedDocument = parseDocument(RESOURCES_DIR_PATH.resolve(location));
    Document resultDocument = removeIgnoredCxmlNodes(parseDocument(resultCxmlPath));
    assertXMLEqual(expectedDocument, resultDocument);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

}

user2490093
  • 69
  • 10
  • Can you print the two documents to xml? Apparently XMLUnit feels they are different. This might be true, and if that is acceptable, use a `DifferenceListener` as shown below by @jsheeran. – Koos Gadellaa Oct 27 '16 at 09:14

2 Answers2

2

This is a result of using the default implementation of DifferenceListener. To specify that a comparison should ignore a particular type of difference, you need to provide your own implementation.

The following example is for XMLUnit 1. I haven't yet used 2, but from what I understand the solution would be much the same.

public class DifferenceListenerImpl implements DifferenceListener {
  @Override
  public int differenceFound(Difference d) {
    if (d.getId() == DifferenceConstants.ELEMENT_NUM_ATTRIBUTES_ID) {
      return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
    }
    // handle other difference types here
  }

  @Override
  public void skippedComparison(Node control, Node test) {
    // not needed
  }
}
jsheeran
  • 2,912
  • 2
  • 17
  • 32
  • is this an abstract class? – user2490093 Oct 28 '16 at 10:04
  • `DifferenceListener` is an interface in XMLUnit 1; its equivalent in 2 is `DifferenceEvaluator`. The implementation is a concrete class. – jsheeran Oct 28 '16 at 10:09
  • When I am creating the DifferenceListener class it is given me an error .Eclipse suggests to convert this into abstract method or add following method public void skippedComparison(Node arg0, Node arg1) { // TODO Auto-generated method stub } – user2490093 Oct 28 '16 at 11:57
  • Implement the method and leave it blank. It's not needed for this purpose. – jsheeran Oct 28 '16 at 12:05
  • I implemented DifferenceListener to existing class and and put the solution code their..Is it the correct way to do? – user2490093 Oct 31 '16 at 11:14
2

For XMLUnit 2.x you wouldn't use DifferenceEvaluator but an "attribute filter" like in

DiffBuilder b = DiffBuilder.compare(some-onput)
    .withTest(some-test)
    .withAttributeFilter(a -> {
        QName attrName = Nodes.getQName(a);
        return !a.equals(new QName("schemaVS"))
            && !a.equals(new QName(schema-uri-of-a, "schemaLocate"));
    })
    ...
Stefan Bodewig
  • 3,260
  • 15
  • 22
  • I have configured xmlunit-1.6.jar in my project ,but DiffBuilder can not be resolved and I used this body within 'removeIgnoredCxmlNodes' method mentioned in the question.Am I correct here? – user2490093 Oct 31 '16 at 09:11
  • There is no `DiffBuilder` in XMLUnit 1.6, that's why I said "in XMLUnit 2.x". If you are forced to stick with the old 1.6 then something like @jsheeran's answer is what you need to do. – Stefan Bodewig Oct 31 '16 at 16:06