Wednesday, June 09, 2010

SoapUI MockOperation Dispatching in Groovy script

SoapUI provides several ways to dispatch mock service response, you can find the detail information from: http://www.soapui.org/Service-Mocking/simulating-complex-behaviour.html.

If you want to manage your mock response dispatch more flexible, then the Script way is your choice.

Here is the sample script you can select your mock response according to the different requests:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());

//Get node value from Xpath
def OID1 = holder.getNodeValue("//v2:getRequest[1]/v2:OIDs[1]");
def OID2 = holder.getNodeValue("//v2:getRequest[1]/v2:OIDs[2]");

//if condition 1 is matched, return mockresopnse1
if (OID1 == "C1308C7C-097F"&& OID2 == "") {

mockOperation.setDefaultResponse("mockresopnse1");

}
//else if condition 2 is matched, return mockresopnse2
else if (OID1 == "A097B4E5-0159" && OID2 == "17001212-AABF") {

mockOperation.setDefaultResponse("mockresopnse2");

}


PS: Due to lack of documentation and sample scripts on this, I spent me several hours to do some investigation on it, the only thing to do is "reading the APIs and have a try".
Hope it helps,And Thanks for Lynn and Vance's help on this as well! :)

1 comment: