2016-11-22 71 views
2

我想在Groovy代碼我用xml文件忽略爲了SOAP響應比較:的Groovy:比較與XML文件的SOAP響應

這裏是我的代碼:

import org.custommonkey.xmlunit.Stuff 
import org.xmlunit.Stuff 

//ExpectedString is my xml converted to text, same for ResponseString 

Diff diff = DiffBuilder.compare(ExpectedString) 
      .withTest(ResponseString) 
      .ignoreComments() 
      .ignoreWhitespace() 
      .checkForSimilar() 
      .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)) 
      .build(); 

assertFalse("XML similar " + diff.toString(), diff.hasDifferences()) 

所以,你可以看到,我用DefaultNodeMatcher,我用XMLUnit2.0 ...沒有結果(甚至不會忽略順序或比較時有異常錯誤)

有沒有解決方案?解決此問題

由於我不顧一切地找到一個直接的,我可以排序我的XML和我的肥皂響應,所以我可以有一個簡單的差異?有沒有辦法按字母順序排列它?如果是,如何?

謝謝你們!

更新:

這裏是我的XML結構簡化

<body> 
<stuff> 
    <miniStuff></miniStuff> 
    <miniStuff></miniStuff> 
</stuff> 
<Services> 
    <Service> 
    <tag1>ValueA</tag1> 
    <tag2>ValueAA</tag2> 
    </Service> 
    <Service> 
    <tag1>ValueB</tag1> 
    <tag2>ValueBB</tag2> 
    </Service> 
</services> 
</body> 

我的問題是我不能保證值a是第一位的,而不是第二

+0

你有樣品數據,你試圖比較嗎? – Rao

+0

我會在幾分鐘內給我的帖子添加一個簡單的數據,謝謝:) –

+0

@Rao:準備好:) –

回答

5

這裏是一個你可能需要:使用ByNameAndTextRecSelector

withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName)) 

單元測試:

@Test 
    public void testDiffOrder() { 
     final String control = """ 
      <r> 
       <ser> 
        <t1>a</t1> 
        <t2>b</t2> 
       </ser> 
       <ser> 
        <t1>d</t1> 
        <t2>e</t2> 
       </ser> 
      </r>""" 
     final String test = """ 
      <r> 
       <ser> 
        <t1>d</t1> 
        <t2>e</t2> 
       </ser> 
       <ser> 
        <t1>a</t1> 
        <t2>b</t2> 
       </ser> 
      </r>""" 
     Diff diff = DiffBuilder.compare(Input.fromString(control)) 
       .withTest(Input.fromString(test)) 
       .ignoreComments() 
       .ignoreWhitespace() 
       .checkForSimilar() 
       .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName)) 
       .build() 

     assertFalse("XML differ " + diff.toString(), diff.hasDifferences()) 
    } 

貸@bodewig。從here

UPDATE 樣本:更groovified版本與有機磷農​​藥原XML片段。

import org.xmlunit.builder.DiffBuilder 
import org.xmlunit.builder.Input 
import org.xmlunit.diff.ByNameAndTextRecSelector 
import org.xmlunit.diff.DefaultNodeMatcher 
import org.xmlunit.diff.ElementSelectors 

def control = """<body> 
    <stuff> 
     <miniStuff /> 
     <miniStuff /> 
    </stuff> 
    <Services> 
     <Service> 
     <tag1>ValueB</tag1> 
     <tag2>ValueBB</tag2> 
     </Service> 
     <Service> 
     <tag1>ValueA</tag1> 
     <tag2>ValueAA</tag2> 
     </Service> 
    </Services> 
</body>""" 
def test = """<body> 
    <stuff> 
     <miniStuff /> 
     <miniStuff /> 
    </stuff> 
    <Services> 
     <Service> 
     <tag1>ValueA</tag1> 
     <tag2>ValueAA</tag2> 
     </Service> 
     <Service> 
     <tag1>ValueB</tag1> 
     <tag2>ValueBB</tag2> 
     </Service> 
    </Services> 
</body>""" 
def myDiff = DiffBuilder.compare(Input.fromString(control)) 
      .withTest(Input.fromString(test)) 
      .checkForSimilar() 
      .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName)) 
      .build() 
println myDiff.toString() 
println myDiff.hasDifferences() 
assert !myDiff.hasDifferences() 
+0

工作好!謝謝bro –

+0

@HamzaAmami,很高興知道這是有幫助的。 – Rao