I am trying to compare two xml only partially using XMLUnit2. I tried below Xpath to compare element int
alone. But my test is failed because, it is checking boolean
tag as well.
How do I make this test pass?
@Test
public void diff_through_xPath(){
String myControlXML = "<struct><boolean>false</boolean><int>3</int></struct>";
String myTestXML = "<struct><boolean>true</boolean><int>3</int></struct>";
ElementSelector childSelector = selectorForElementNamed("int", byXPath("//int", byName));
Diff myDiffSimilar = DiffBuilder.compare(myControlXML).withTest(myTestXML)
.withNodeMatcher(new DefaultNodeMatcher(childSelector, byName))
.checkForSimilar()
.ignoreWhitespace()
.build();
assertFalse("XML similar " + myDiffSimilar.toString(),
myDiffSimilar.hasDifferences());
}
Edit: With NodeFilter, I removed the unwanted nodes from comparison. But is there a way to give Xpath, and compare only the nodes evaluated by XPath.
@Test
public void diff_through_xPath() {
String myControlXML = "<struct><boolean>false</boolean><int>3</int></struct>";
String myTestXML = "<struct><boolean>true</boolean><int>3</int></struct>";
Diff myDiffSimilar = DiffBuilder.compare(myControlXML).withTest(myTestXML)
.withNodeFilter(new Predicate<Node>() {
public boolean test(Node node) {
return !node.getNodeName().equals("boolean"); //ignores all child nodes from of 'a'.
}
})
//.checkForSimilar()
.ignoreWhitespace()
.build();
assertFalse("XML similar " + myDiffSimilar.toString(),
myDiffSimilar.hasDifferences());
}