Java:XMLUnit.setIgnoreWhitespace(true)导致奇怪的输出

时间:2015-09-02 13:33:28

标签: java xml unit-testing xmlunit

我在以下代码中遇到了问题。

String xml1="<Akash><status>COMPLETE</status>"
                    +"<startedTime>23</startedTime></Akash>"; 
String xml2="<Akash><status>COMPLETE</status><startedTime></startedTime></Akash>";

Diff diff = new Diff(xml1,xml2);
diff.overrideElementQualifier(new ElementNameAndTextQualifier()); 
assertTrue("XML similar " + diff.toString(), diff.similar());

当我不在上面的java代码中写下这一行时。

 XMLUnit.setIgnoreWhitespace(true) 

Assert在“开始时间”附近给出了一个异常,即该节点的值不存在,这就是我想要的,因为值23不存在。但是当我在实例化Diff类之前将这一行(setIgnoreWhitespace)添加到代码中时,尽管有相同的xml,但是这段代码通过了断言测试。

我真的没有得到****在这里发生的事情。请帮帮我。

注意:我需要XMLUnit.setIgnoreWhitespace(true)来等同两个xml,它们的某些节点之间有空格,否则代码会产生其他错误。

1 个答案:

答案 0 :(得分:1)

public static void main(String[] args) throws Exception {
    String xml1="<Akash><status>COMPLETE</status>"
        +"<startedTime>23</startedTime></Akash>"; 
    String xml2="<Akash><status>COMPLETE</status><startedTime></startedTime></Akash>";

    XMLUnit.setIgnoreWhitespace(true);
    Diff diff = new Diff(xml1,xml2);
    diff.overrideElementQualifier(new ElementNameAndTextQualifier()); 
    assertTrue("XML similar " + diff.toString(), diff.similar());
}

为我抛出异常(XMLUnit 1.6)。

Exception in thread "main" java.lang.AssertionError: XML similar org.custommonkey.xmlunit.Diff
[different] Expected presence of child nodes to be 'true' but was 'false' - comparing <startedTime...> at /Akash[1]/startedTime[1] to <startedTime...> at /Akash[1]/startedTime[1]
相关问题