开发者

SoapUI Groovy Scripts

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?


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


Again, I appreciate this is old, but Sinnerinc's answer above doesn't solve the original problem, because his solution will still suffer from a NPE because mockRequest was null.

I have a related issue, and found this post which suggests the mockResponse will be null if the mock service has never served a request and you click on the green triangle button to run the script!


In the SmartBear Forum solution code, the green 'play' button pops up the 'mockRequest is Null' warning, because the mock request object is not def'd up.

The mockRequest object is defined when a 'real' test executes the MockService endpoint.

To test code I put the following Test Code and click 'play' until I am happy with my coverage.

Then I send a Test Step to call the MockService endpoint

Here is the code:

def mockRequestrequestContent = "" 
if (mockRequest != null) 
    mockRequestrequestContent = mockRequest.requestContent
else 
    mockRequestrequestContent = "<testRequestXmlOrJson/>" 
log.info(mockRequestrequestContent) 

//begin script like @sinnerinc's above

Note: The current version of SoapUI 5.50 doesn't have a log window at the bottom, trying to gather information is challenging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜