Using Groovy scripts to select Dispatch actions in soapUI

You can use the power of Groovy for selecting MockResponses in soapUI
If your request is of the format :-


You can use the following script :-

def groovyUtils = new
def req = new XmlSlurper().parseText(mockRequest.requestContent) "Customer #${}"
context.result =

Here we are using mockRequest.requestContent to get the request soap envelope. We then use the XmlSlurper to parse this. XmlSlurper does not care about namespaces and it can parse XML into a document tree that may be traversed similar to XPath expressions. Doc :

context.result ensures that the MockResponse whose name =CustomerNumber (in this case 1234) is selected as a response. is for logging and can be tested in the “script log” tab below when you send a request

Other variables that can be used are : mockOperation to select responses based on operation, requestContext and context.

You can have fancy scripts which contain if statements, connect to database, files, inlist etc whatever Groovy supports!

You may also pass back calculations by parsing parameters from request and converting them:-

Sample Response

<soapenv:Envelope xmlns:soapenv="" xmlns:typ=>

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }


def request = new XmlSlurper().parseText(mockRequest.requestContent)
def a = request.Body.Add.x.toDouble()
def b = request.Body.Add.y.toDouble()
context.result = a + b

You can use XmlHolder as well if you care for namespaces:-


<soapenv:Envelope xmlns:soapenv="" xmlns:tem="">


def groovyUtils = new context )
def holder = groovyUtils.getXmlHolder( mockRequest.requestContent ) 
holder.declareNamespace( 'tem', '')

context.Facility = holder.getNodeValue( "//tem:Facility")
context.EntityID = holder.getNodeValue( "//tem:EntityID")

But in this case you will have to explicitly declare namespaces.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s