如何在SoapUI MockService中返回动态响应

时间:2010-05-27 11:10:34

标签: soapui

我是SoapUI的新手,刚刚配置了一个非常简单的MockService。是否可以操纵响应,以便对特定请求动态构建响应的元素?

情景1:

请求:

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

响应:

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

情景2:

请求:

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

响应:

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

这是一个简单的测试,我不需要它比上面做更多。我目前正在做以下事情,产生我想要的结果,但由于我对此完全陌生,我相信有更好的选择:

响应:

<response>
  ${dynElement}
</response>

Groovy脚本:

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)
}

2 个答案:

答案 0 :(得分:3)

您也可以使用XmlSlurper而不是xpath。

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

this question's的答案中无耻地撕掉,请不要伤害我。

答案 1 :(得分:2)

我使用“预设响应”和xpath查询。为此,您将在模拟服务中设置一系列调度处理程序,以使请求与响应匹配。假设你有Request1,Request2,Response1,Response2。使用“查询匹配”分派方法来匹配XPATH表达式。在该匹配项上,返回所需的响应。即如果在xpath中找到ID1,则返回预设的Response1。

此外,PRO版本在XPATH方面表现非常出色,因此您无需手动编写代码。即它可以查看响应点击你想要触发的事物(在你的情况下,ID1),它会为你构建XPATH表达式。我基于此请求了PRO许可证。目前正在等待预算......

IMO,比开始使用Groovy更容易上手。

克里斯