SoapUI:使用Groovy根据请求的验证返回MockResponse

时间:2014-11-17 14:33:49

标签: web-services soap groovy soapui

在SoapUI(以及SOAP Web服务)中,如何根据请求评估字符串并根据该请求的条件返回响应?

到目前为止,我能够读取请求中的值并在响应中使用这些值,但如果我尝试将其扩展为基于if语句的响应,则不会返回任何值。任何帮助,将不胜感激。到目前为止,这是我的代码:

// create XmlHolder for request content
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )

// get arguments from the request (this looks like it is all working correctly)
def FirstName = holder.getNodeValue("//FirstName")
def LastName =  holder.getNodeValue("//LastName") 
def Phone = holder.getNodeValue("//Phone")
def Gender =  holder.getNodeValue("//Gender") 
def Age =  holder.getNodeValue("//Age") 

//here are where the problems start 
//I am using an evaluation in the if statement that should always be true
//I was trying something closer to the end solution I would like eg. if(Gender == "M")
//but this also failed to return any response 
def response
if("M" == ("M")){
    reponse = FirstName;
    //return response
    //context.setProperty("responseMessage", response)
}else
{
    response = "error"
    //return response
    //context.setProperty("responseMessage", response)
}
    return response

context.setProperty("responseMessage", response)

我已经留下了一些我尝试的其他事情

由于

编辑:这是在SoapUI MockResponse中,我想在${responseMessage}中设置MockReponse作为XML中响应的值。我的目标是向模拟服务发送请求,并能够根据请求中包含的字符串进行响应,例如是否"性别"是" M"或者" F",年龄在某个范围内等。我觉得如果我的问题中的代码问题得到解决以便返回一个值,那么我可以自己解决扩展部分

感谢albciff,这解决了我的问题:

...
if("M" == ("M"))
{
    requestContext.responseMessage = FirstName
}else
{
    requestContext.responseMessage ="error"
}

1 个答案:

答案 0 :(得分:2)

我想你已经有了以下情景。

您正在SOAPUI中创建MockServiceMochService内有一个operation,其MockResponse并且您从operation发送回复作为脚本。在以下图片中显示了MockServiceoperationMockResponse

enter image description here

因此,例如,如果您的MockResponse包含以下内容:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>${responseMessage}</soap:Body>
</soap:Envelope>

在填充${responseMessage}的脚本中,您必须通过requestContext对象分配属性值,而不是在context中设置属性或返回值,在这种情况下{{1} }。

在您的脚本中,这可能是:

requestContext.responseMesssage

您还可以查看SOAPUI MockService documentation

希望这有帮助,