2009-11-24 141 views
2

我试图读取传入的请求&根据请求中的值soapUI 3.0设置模拟响应。为此,我使用以下groovy脚本。SoapUI Groovy脚本

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"); 
} 

但它不工作,抱怨mockRequest对象为空:

com.eviware.soapui.impl.wsdl.mock.DispatchException:无法使用脚本派遣;显示java.lang.NullPointerException:不能空对象

上调用方法getContentElement(),但我已经使用了类似的一种代码soapUI 2.0版本和成功。我怎样才能解决这个问题?

回答

2

我知道这个问题很老,但我昨天遇到了同样的问题,这里是我设法使用groovy脚本调度响应(请注意,这是我第一次使用soapUI和groovy,因此可能在那里将是更好的方法来做到这一点)。

// 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 
2

同样,我很欣赏这是旧的,但Sinnerinc的回答上面并没有解决原来的问题,因为他的解决方案仍将从NPE受到影响,因为mockRequest为空。

我有一个相关的问题,发现this post这表明如果模拟服务从未提供请求,并且您单击绿色三角形按钮运行脚本,则mockResponse将为null!