SoapUI模拟服务自定义响应序列

时间:2015-10-06 16:42:00

标签: groovy response soapui

我有一个带有多个响应的SoapUI模拟服务。我想为我的回复定义一个自定义序列,我不一定在这个特定的测试中使用所有的响应。

我实际上已经设法让它在过去工作,但我认为在更新版本的产品中有些变化,而且功能已停止工作。那是一个SOAP Web服务。现在我正在嘲笑一个RESTful Web服务,我有同样的要求来帮助我进行测试。

SEQUENCE调度选项不是我想要的,因为它将按创建顺序返回所有定义的响应。 SCRIPT选项是我之前使用的选项,但现在我可以实现的是定义一个要生成的响应。对于这个测试,我没有兴趣检查请求的某些内容,以决定发回哪个响应。

例如,如果我定义了8个响应,我只想指定返回以下响应: -

响应#2,然后响应#3,然后响应#4,最后响应#7;因此不使用响应#1,#5,#6和#8。

我的问题在SmartBear论坛中详细提出: - simple scripting in a Mock Service - no longer works

1 个答案:

答案 0 :(得分:1)

我尝试使用带有响应顺序的连续返回语句在SOAPUI论坛中发帖,但它不起作用。

我不打算将您的groovy代码作为DISPATCH脚本,而是使用以下groovy代码作为解决方法,其中包括使用列表来保持您的响应按所需顺序并将此列表保留在context中每次使用以下代码更新它:

// get the list from the context
def myRespList = context.myRespList

// if list is null or empty reinitalize it
if(!myRespList || !myRespList?.size){   
    // list in the desired output order using the response names that your
    // create in your mockservice
    myRespList = ["Response 2","Response 3","Response 4","Response 7"]  
}
// take the first element from the list
def resp = myRespList.take(1)
// update the context with the list without this element
context.myRespList = myRespList.drop(1)
// return the response
log.info "-->"+resp
return resp

此代码按预期工作,因为context保留了列表,每次此脚本返回下一个响应时,当列表为空时,它会重新填充它,然后以相同的顺序重新启动循环。

作为我使用此mockService的插图,我得到了以下脚本日志:

enter image description here

修改

如果作为OP,您的SOAPUI版本出现问题,因为返回的字符串位于方括号之间,即:[Response 1],请使用以下命令更改元素从数组中获取的方式:

// take the first element from the list
def resp = myRespList.take(1)[0] 

而不是:

// take the first element from the list
def resp = myRespList.take(1)

请注意[0]

通过此更改,返回字符串将为Response 1而不是[Response 1]

在这种情况下,脚本将是:

// get the list from the context
def myRespList = context.myRespList

// if list is null or empty reinitalize it
if(!myRespList || !myRespList?.size){   
    // list in the desired output order using the response names that your
    // create in your mockservice
    myRespList = ["Response 2","Response 3","Response 4","Response 7"]  
}
// take the first element from the list
def resp = myRespList.take(1)[0]
// update the context with the list without this element
context.myRespList = myRespList.drop(1)
// return the response
log.info "-->"+resp
return resp

希望这有帮助,