0

In SoapUI 5.2.1 I am trying to apply assertions within my Mock Dispatch Groovy Script to compare that the XML received by the mock, matches my expected xml. I have seen references to using XMLUnit to achieve this. Does anyone have a full groovy script including:

  1. Import of required libraries
  2. Accessing the xml payload in a format that XMLUnit can understand
  3. Creating an expected xml payload that the request payload will be compared with
  4. Comparing the xml payloads in an xml aware manner, presumably using XMLUnit
  5. Generating the assertion failures or taking some other action

There are some other questions around this area, but all appear incomplete to me.

Thanks, Matt.

MattG
  • 5,589
  • 5
  • 36
  • 52

1 Answers1

0

I have got it working, thanks to @Nick Grealy. Some notes:

  1. Omit the XML declaration from the inline xml "expectedRequest" or you get an exception with 'The processing instruction target matching "[xX][mM][lL]" is not allowed.'
  2. You need to define the 2 response mock messages:
    • FailureResponse
    • SuccessResponse

Here is the groovy code inside SOAP UI v 5.2.1, Mock Dispatch script

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
MattG
  • 5,589
  • 5
  • 36
  • 52