Deminish
Deminish

Reputation: 23

SoapUI: Using Groovy to return a MockResponse based on validation of the request

In SoapUI (and a SOAP web service), how do you evaluate a string from a request and return a response based on conditions around that request?

So far, I am able to read the values in the request and use those values in the response, but if I try to extend this to a response based on an if statement then no value is returned. Any help would be appreciated. Here is my code so far:

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

I have left in as comments some of the other things I have tried

Thanks

EDIT: This is within the SoapUI MockResponse, I want to set ${responseMessage} in the MockReponse as the value of the response in the XML. My objective is to send a request to the mock service and to be able to respond based on the strings contained in the request, such as whether "Gender" is "M" or "F", age is within some range etc. I feel that if my issue with the code in the question is resolved so that a value is returned, then I can solve the extended parts myself

Thanks to albciff, this has solved my problem:

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

Upvotes: 2

Views: 5105

Answers (1)

albciff
albciff

Reputation: 18507

I suppose that you've the follow scenario.

You're creating a MockService in SOAPUI, inside MochService there is an operation which has a MockResponse and you dispatch your response from the operation as script. In the follow image the MockService, operation and MockResponse are shown:

enter image description here

So if for example you have a MockResponse with the follow content:

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

In your script to fill ${responseMessage} you have to assign the property value through requestContext object instead of setting property in the context or returning the value, in this case requestContext.responseMesssage.

In your script this could be:

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

You can also take a look at SOAPUI MockService documentation

Hope this helps,

Upvotes: 2

Related Questions