tags:

views:

338

answers:

2

Hi,

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")
}
+1  A: 

I do it with "canned responses" and xpath queries. To do this, you'll set up a series of dispatch handlers in the mockservice to match requsts with respones. Suppose you've got Request1, Request2, Response1, Response2. Use the "Query Match" dispatch method, to match an XPATH expression. On that match, return the desired response. i.e. if you find ID1 in the xpath, return the canned Response1.

Also, the PRO version does a great job with the XPATH, so you don't have to hand code it. i.e. it can look at a response click on the thing that you want to trigger on (in your case, ID1) and it builds the XPATH expression for you. I requested a PRO license, just based on that. Currently awaiting budgeting...

IMO, much easier to get started than figuring out groovy.

Chris

Chris Thornton
Thanks Chris, that is what I was looking for. I like the approach of setting up 'canned responses' and the XPath stuff seems to work for me. I am very impressed with this tool so far.
Ross