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 requestThis 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.
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
b. Import wsdl into the project
c. Here we can see operation is "doAction". Right click on request and create mock service.
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.
e. Already I have created two responses, creating one more response (R3) for operation.
f. Responses R1, R2 and R3 are ready
g. Now step is to write script for to handle request. Click on operation and change dispatch to SCRIPT.
h. Copy that script whatever I have provided before creating SOAP project.
i. Change the target URL for request to MultipleResponse
j. Provide URL
k. Start the mock server for "MultipleResponses".
l. This is my R1 response
m. Execute request