2013-05-22 70 views
1

我正在使用xmlunit来比较两个文本文件。控件的XML是:XmlUnit和子节点

<books> 
    <book> 
     <name>Angels &amp; Demons</name> 
     <isbn>9971-5-0210-0</isbn> 
     <author>Dan Brown</author> 
     <category></category> 
    </book> 
</books> 

我比较这与另一块xml具有和元素交换。

<books> 
    <book> 
     <isbn>9971-5-0210-0</isbn> 
     <name>Angels &amp; Demons</name> 
     <author>Dan Brown</author> 
     <category></category> 
    </book> 
</books> 

的DIFF对象报告下列区别:

Expected sequence of child nodes '1' but was '3' - comparing <name...> at /books[1]/book[1]/name[1] to <name...> at /books[1]/book[1]/name[1] 

如果<name>是子节点',不会<isbn>是子节点 '2'?

+0

好像XMLUnit测试在XML子节点计数回车。设置以下内容\t \t ** XMLUnit.setIgnoreWhitespace(true); **给出了'期望的子节点序列'0'的更直观结果,但是'1' - 比较了/ struct [1]/int [1]处的 at/struct [1]/int [1]' – timmy

回答

0

看起来像XmlUnit计数在xml中的回车作为子节点。设置以下XMLUnit.setIgnoreWhitespace(true);给的子节点 '0' 的预期序列的更直观的结果,但为 '1' - 比较<int...> at /struct[1]/int[1] to <int...> at /struct[1]/int[1]

1

对于XMLUnit测试2.X版本:

对于XMLUnit测试2.xx的版本中,XMLUnit测试。 setIgnoreWhitespace(true)不再适用。现在,您可以通过添加DiffBuilder.ignoreWhitespace()将“忽略空白”直接添加到DiffBuilder中。

Diff diffXml =DiffBuilder.compare(expectedXml).withTest(actualXml).normalizeWhitespace().checkForSimilar().build(); 

断言xml的类似,你可以例如。这样做:

MatcherAssert.assertThat(diffXml.toString(), is("[identical]")); 

有关的1.x之间变化2.X furhter信息,请参阅: https://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to-2.x