Selecting a response based on the request using SOAP UI

To mock a service, Everyone knows based on request we can give static response always. Here I want to show that how can we create dynamic multiple responses using SOAP UI based out of request.

Selecting a response based on the request

This script is specified at the MockOperation level and uses the script code to extract the input values from the incoming request. Based on some validations it returns the name of the MockResponse to return to the client. The script is as follows:
Aim of Script: In request we have name property based name property we are returning different responses like R1, R2 and R3.

  • If name is "AA" then response R1
  • Else if name is "BB" then response R2
  • Else response is R3.
def temp="XX";
def groovyUtils=new com.eviware.soapui.support.GroovyUtils(context)
def xml=new XmlSlurper().parseText(mockRequest.requestContent)
xml.breadthFirst().each{
        def v=it.toString()
        log.info("==============="+it.name()+"==================="+it.text());
        if(it.name()=="name"){
                temp=it.text();
                log.info("===================matching tag=========================="+it.text());
        }
}
//log.info("*temp**"+temp);
if(temp=='AA'){
        log.info("if R1");
        mockOperation.setDefaultResponse("R1");
}else if(temp=='BB'){
        log.info("else if Response 1");
        mockOperation.setDefaultResponse("R2");
}else{
        log.info("else Response 1");
        mockOperation.setDefaultResponse("R3");
}

a.Create SOAP Project in SOAPUI

Soap ui project

b. Import wsdl into the project

soapui import wsdl

c. Here we can see operation is "doAction". Right click on request and create mock service.

soaupui mock service using doaction

d. Mock service name is given as MultipleResponses. Our aim to create multiple responses for single request. Click on operation and create responses like below.

create soapui mock resonse

e. Already I have created two responses, creating one more response (R3) for operation.

soapui multiple mock resopnses for single request

f. Responses R1, R2 and R3 are ready

soapui multiple mock resopnses are ready

g. Now step is to write script for to handle request. Click on operation and change dispatch to SCRIPT.

soapui script to handle multiple response

h. Copy that script whatever I have provided before creating SOAP project.

paste scipt in soapui script console

i. Change the target URL for request to MultipleResponse

change soap ui target url

j. Provide URL

provide soapui url

k. Start the mock server for "MultipleResponses". start soapui mockservice server

l. This is my R1 response

soapui first response

m. Execute request

soap ui Execute request