XMLUnit:无法使用overrideDifferenceListener附加自定义DifferenceListener

时间:2014-01-25 15:48:08

标签: java xmlunit

我无法使用overrideDifferenceListener方法附加自定义差异侦听器。

    String xml1 = readFile("data/xml1.xml", Charset.defaultCharset());
    String xml2 = readFile("data/xml2.xml", Charset.defaultCharset());

    Diff xmlDiff = new Diff(xml1, xml2);

    xmlDiff.overrideDifferenceListener(new MyDiffListener(listConsideredForDiff));
    assertTrue(xmlDiff.similar());

自定义差异类

class MyDiffListener implements DifferenceListener{
    private Set<String> consideredList = new HashSet<String>(); // list to consider for difference

    public MyDiffListener(ArrayList<String> list) {

        for(String element: list){
            consideredList.add(element);
        }

    }

    @Override
    public int differenceFound(Difference diff) {

        if (diff.getId() == DifferenceConstants.TEXT_VALUE_ID){
            if(!consideredList.contains(diff.getControlNodeDetail().getNode().getParentNode().getNodeName())){
                return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
            }
        }
        return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;

    }

    @Override
    public void skippedComparison(Node arg0, Node arg1) {
        // TODO Auto-generated method stub

    }

}
当我调试时,

程序控制没有在MyDiffListener的differenceFound方法中找到断点。

这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

这应该有效:

    String xml1 = "<root><a/></root>";
    String xml2 = "<root><b/></root>";

    Diff xmlDiff = new Diff(xml1, xml2);
    xmlDiff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            public int differenceFound(Difference diff) {
                System.err.println("called: " + diff);
                return RETURN_ACCEPT_DIFFERENCE;
            }
            @Override
            public void skippedComparison(Node arg0, Node arg1) { }
        });
    xmlDiff.similar();

发射

called: Expected element tag name 'a' but was 'b' - comparing <a...> at /root[1]/a[1] to <b...> at /root[1]/b[1]

也许您的文档中没有任何差异,或者您的断点正在查看错误的行?