Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return dynamic response in SoapUI MockService

Tags:

soapui

I am new to SoapUI and have just configured a very simple MockService. Is it possible to manipulate the response so that for a particular request the response's elements are dynamically built up?

Scenario 1:

Request:

<record>
  <identifier>ID1</identifier>
</record>

Response:

<response>
  <child1>child 1</child1>
</response>

Scenario 2:

Request:

<record>
  <identifier>ID2</identifier>
</record>

Response:

<response>
  <child2>child 2</child2>
</response>

It is for a simple test and I don't need it to do any more than the above. I am currently doing the following that yields the results I want but since I am completely new to this I am sure there are better alternatives:

Response:

<response>
  ${dynElement}
</response>

Groovy script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
def reqRef = String.valueOf(holder.getNodeValue("//identifier"))

def child1Text = "<child1>child 1</child1>"
def child2Text = "<child2>child 2</child2>"

if (reqRef == "ID1") {
  context.setProperty("dynElement", child1Text)
} else if (reqRef == "ID2") {
  context.setProperty("dynElement", child2Text)
}
like image 469
Ross Avatar asked May 27 '10 11:05

Ross


1 Answers

Instead of xpath, you could also use XmlSlurper.

def req = new XmlSlurper().parseText(mockRequest.requestContent)
def reqRef = req.record.identifier

Shamelessly ripped of from this question's answers, please don't hurt me.

like image 198
0x89 Avatar answered Oct 12 '22 12:10

0x89