SoapUI - 使用XMLUnit& amp;在模拟调度脚本中比较XML。 Groovy的

时间:2016-11-28 01:00:42

标签: xml groovy soapui xmlunit

在SoapUI 5.2.1中我试图在我的Mock Dispatch Groovy脚本中应用断言来比较mock接收的XML与我期望的xml匹配。我已经看到使用XMLUnit来实现这一点的参考。有没有人有一个完整的groovy脚本,包括:

  1. 导入所需的库
  2. 以XMLUnit可以理解的格式访问xml有效内容
  3. 创建预期的xml有效负载,将请求有效负载与
  4. 进行比较
  5. 以xml感知方式比较xml有效负载,可能是使用XMLUnit
  6. 生成断言失败或采取其他行动
  7. 这个领域还有其他一些问题,但对我来说都不完整。

    谢谢, 太

1 个答案:

答案 0 :(得分:0)

感谢@Nick Grealy,我已经开始工作了。一些说明:

  1. 从内联xml" expectedRequest"中省略XML声明。或者你得到一个例外,处理指令目标匹配" [xX] [mM] [lL]"不允许。'
  2. 您需要定义2个响应模拟消息:
    • FailureResponse
    • SuccessResponse
  3. 这是SOAP UI v 5.2.1,Mock Dispatch脚本

    中的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