SoapUI - 使用XMLUnit和Groovy在模拟调度脚本中比较XML

问题描述:

在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? –

谢谢@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