Getting SOAPUI to Pass Back a Value froma Request Using a Mock Service

If need a Mock Service to reply to a SOAP request with a value from that request, here’s the Groovy script to do it. (Groovy is a scripting language that can be run in the Java JVM. SOAPUI has it built in.)

1) Select your Mock Service in SOAPUI
2) Select the Mock Service operation
3) Select the appropriate Mock Service response
4) Click the “Script” tab at the bottom of the response editor window
5) Paste the following code into the script editing pane, substituting your values

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def id = String.valueOf(holder.getNodeValue("THE NODE CONTAINING THE VALUE YOU WANT TO REPLY WITH HERE")
Context.setProperty("PARAMETER NAME HERE", id)

So basically you’re pulling the value from the node you define in line 3, into the variable called “id”. You’re then assigning the value of “id” to the parameter you define in line 4. You then insert the parameter into your response message and viola!

For example if the node you wanted to pull the value from was a node called “Name”, and the parameter you wanted to use was “nameid” then your code would look like this :

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def id = String.valueOf(holder.getNodeValue(“Name”))
context.setProperty(“nameid”, id)

You’d then put ${nameid} into your MockService response.

Leave a Reply