2015-10-06 84 views
0

我有一个带有多个响应的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

+0

@albciff - 这是一个有趣的问题。你的解决方案没有回答我的问题;它并没有真正的工作。 (请仔细阅读并查看我在列表中分配字符串的方式时发现的奇怪之处)。然而,这非常有帮助,并构成了我解决方案的中坚力量。 – jamescollett

+0

@albciff亲爱的同事,我应该很乐意为您提供荣誉,但我也注意到您的解决方案完全如所提供的那样存在缺陷。方括号不是词典风格的选择;由于Groovy字符串操作的奇怪效果,它们是必需的。没有它们,它不起作用。至少对我来说这是奇怪的。我以为我明确表示了。由于除了接受它作为解决方案外,没有办法为您的贡献付款,所以我会做这件事,但其他读者注意到必要的改动。我希望能减轻你的烦恼。 – jamescollett

+0

我不在乎答案是否被接受......既不是关于荣誉(它是什么意思?)我对你的评论感到愤怒......可能我过度反应,但答案对我有用,我感到有点生气(太多了)......我想为我的反应道歉......当然,如果你觉得这确实不能解决你的问题,那么你可以自由地接受这个问题。 – albciff

回答

1

我尽可能尝试使用连续的语句返回与响应顺序在SOAPUI论坛张贴这是行不通的。

而不是你的groovy代码作为一个调度脚本我的目的是使用以下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 

希望这有助于