SoapUI Groovy Scripts

Priyal85 picture Priyal85 · Nov 24, 2009 · Viewed 9.8k times · Source

I'm trying to read the incoming request & set the mock response depending on a value comming in the request in soapUI 3.0. I use the following groovy script for this.

def typeElement = mockRequest.getContentElement().execQuery("//ProductType");
def records =  new XmlParser().parseText(typeElement[0].xmlText())
if (records.text()=="15"){
    mockOperation.setDefaultResponse("Response 2");
} else {
    mockOperation.setDefaultResponse("Response 1");
}

But it does not work, complaining that mockRequest object is null:

com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException: Cannot invoke method getContentElement() on null object

but I've used similar kind of code with soapUI 2.0 version and was successful. How can I fix this?

Answer

sinnerinc picture sinnerinc · Aug 25, 2010

I know this question is pretty old, but I came across the same problem yesterday and here's how I managed to dispatch responses using groovy script (please note, this is the first time I used both soapUI and groovy, thus probably there will be better ways to do that).

    // define request
    def request = new XmlSlurper().parseText(mockRequest.requestContent);
    def resultingResponse = "none"

    //when missing password
    def Password = request.Body.CreateUser.user.Password
    if(Password == '') {
        resultingResponse = 'MissingPassword'
    }

    //when missing firstname
    def Firstname = request.Body.CreateUser.user.FirstName
    if(Firstname == '') {
        resultingResponse = 'MissingFirstname'
    }

context.ResultResponse = resultingResponse