2016-11-28 50 views
0

在SoapUI 5.2.1中,我嘗試在我的模擬調度Groovy腳本中應用斷言,以比較由模擬接收的XML與我預期的xml匹配。我見過使用XMLUnit來實現這一點的參考。有沒有人有一個完整的Groovy腳本包括:SoapUI - 使用XMLUnit和Groovy在模擬調度腳本中比較XML

  1. 導入所需的庫
  2. 訪問的格式的XML有效載荷XMLUnit測試可以理解
  3. 創建該請求有效載荷將與
  4. 進行比較的預期XML有效載荷的
  5. 比較在一個XML感知方式的XML負載,想必使用XMLUnit
  6. 產生斷言失敗或採取其他一些行動

這方面還有其他一些問題,但對我來說都不完整。

謝謝, 馬特。

+0

Hi @MattG,你可以請你提供一個樣本,顯示你已經發現了什麼d? –

回答

0

謝謝@Nick Grealy,我有它的工作。一些注意事項:

  1. 從內聯XML「expectedRequest」省略XML聲明或者你得到一個異常「的處理指令目標匹配‘[XX] [MM] [11]’是不允許的。」
  2. 您需要定義2響應模擬消息:
    • FailureResponse
    • SuccessResponse

這裏是SOAP UI v 5.2.1,模擬調度腳本中的Groovy代碼

import org.custommonkey.xmlunit.* 

XMLUnit.setIgnoreWhitespace(true) 
XMLUnit.setIgnoreComments(true) 
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true) 
XMLUnit.setNormalizeWhitespace(true) 

def expectedRequest = ''' 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> 
    <soap:Body xmlns:m="http://www.example.org/stock"> 
     <m:GetStockPrice> 
       <m:StockName>IBM</m:StockName> 
     </m:GetStockPrice> 
    </soap:Body> 
</soap:Envelope> 
''' 

def actualRequestReceived = mockRequest.requestContent 

def diff = new Diff(actualRequestReceived, expectedRequest) 

diff.compare() 

log.info('actualRequestReceived:' + actualRequestReceived) 
log.info('expectedRequest:' + expectedRequest) 
log.info('identical:' + diff.identical()) 
log.info('similar:' + diff.similar()) 

if (!diff.identical) { 
    responseToUse = "FailureResponse" 
} else { 
    responseToUse = "SuccessResponse" 
} 

return responseToUse 
相關問題