views:

412

answers:

1

Hi , 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 do 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 simillar kind of code with soapUI 2.0 version and was successfull.

Please help me on this matter.

A: 

Hi, 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