我正在尝试仅部分使用 XMLUnit2 比较两个 xml。我尝试在 Xpath 下面int
单独比较元素。但是我的测试失败了,因为它boolean
也在检查标签。
我如何使这个测试通过?
@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());
}
编辑: 使用 NodeFilter,我从比较中删除了不需要的节点。但是有没有办法给出 Xpath,并且只比较由 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());
}