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

SoapUI Mock Service Custom Sequence of Responses

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

我过去确实设法让它工作,但我认为产品的新版本发生了一些变化,该功能停止工作。那是一个 SOAP 网络服务。现在我正在模拟一个 RESTful 网络服务,我有同样的要求来帮助我做我的测试。

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

例如,如果我定义了 8 个响应,我只想能够指定以下响应是 returned:-

响应#2,然后是响应#3,然后是响应#4,最后是响应#7;以便不使用响应 #1、#5、#6 和 #8。

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

我在 SOAPUI 论坛中尝试使用连续的 returns 语句和响应顺序,但它不起作用。post。

我打算使用以下 groovy 代码作为解决方法,而不是将您的 groovy 代码用作 DISPATCH 脚本,其中包括使用列表来使您的响应保持所需的顺序,并且将此列表保存在 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 保留列表并且每次此脚本 return 是下一个响应,当列表为空时它会重新填充它并再次重新启动循环同样的顺序。

作为示例,当我使用这个 mockService 时,我得到以下脚本日志:

编辑

如果作为 OP,您的 SOAPUI 版本有问题,因为 returned 字符串位于方括号之间,例如:[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]

通过此更改,return 字符串将变为 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

希望这对您有所帮助,