0

我正在尝试仅部分使用 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());


    }
4

1 回答 1

0

我不知道这是否是正确的方法。但无论如何都在这里发布以获取您的评论。这样,我只能比较 XPath 评估的 XML 节点列表。

@Test
    public void testXpath() throws Exception {

        File controlFile = new File("src/test/resources/myControlXML.xml");
        File testFile = new File("src/test/resources/myTest.xml");


        Diff myDiff = DiffBuilder.compare(Input.fromDocument(getDocument("src/test/resources/myControlXML.xml", "/struct/int")))
                .withTest(Input.fromDocument(getDocument("src/test/resources/myTest.xml", "/struct/int")))
                .checkForSimilar().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
                .ignoreWhitespace()
                .build();

        assertFalse("XML similar " + myDiff.toString(),
                myDiff.hasDifferences());
    }

    public Document getDocument(String fileName, String xpathExpression) throws Exception {
        File controlFile = new File(fileName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        dBuilder = dbFactory.newDocumentBuilder();

        Document controlDoc = dBuilder.parse(controlFile);
        controlDoc.getDocumentElement().normalize();

        XPath xPath = XPathFactory.newInstance().newXPath();

        NodeList nodes = (NodeList) xPath.compile(xpathExpression).evaluate(
                controlDoc, XPathConstants.NODESET);

        Document newXmlDocument = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder().newDocument();
        Element root = newXmlDocument.createElement("root");
        newXmlDocument.appendChild(root);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            Node copyNode = newXmlDocument.importNode(node, true);
            root.appendChild(copyNode);
        }

        return newXmlDocument;
    }
于 2017-09-26T07:31:05.200 回答