How to return a dynamic response in SoapUI MockService

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

Scenario 1:

Inquiry:

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

Answer:

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

Scenario 2:

Inquiry:

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

Answer:

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

This is for a simple test, and I don’t need to do it anymore than above. I am currently doing the following which gives the results I want, but since I am completely new to this, I am sure there are better alternatives:

Answer:

<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)
}
+5
source share
2 answers

" " xpath. , mockservice, requsts respones. , Request1, Request2, Response1, Response2. " ", XPATH. . , ID1 xpath, Response1.

, PRO- XPATH, . , , ( , ID1), XPATH . PRO, . ...

IMO, , groovy.

+2

xpath XmlSlurper.

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

, , .

+3

All Articles