【问题标题】:SoapUI Groovy ScriptsSoapUI Groovy 脚本
【发布时间】:2010-12-19 18:27:18
【问题描述】:

我正在尝试读取传入请求并根据soapUI 3.0 中的请求中的值设置模拟响应。为此,我使用以下 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对象为null:

com.eviware.soapui.impl.wsdl.mock.DispatchException:使用脚本调度失败; java.lang.NullPointerException:无法在 null 对象上调用方法 getContentElement()

但我已经使用soapUI 2.0 版本的类似代码并且成功了。我该如何解决这个问题?

【问题讨论】:

    标签: groovy mocking soapui groovy-console


    【解决方案1】:

    我知道这个问题已经很老了,但是我昨天遇到了同样的问题,这就是我如何使用 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!

      【讨论】:

        【解决方案3】:

        SmartBear Forum解决方案代码中,绿色的“播放”按钮弹出“mockRequest is Null”警告,因为mock request对象没有被定义。

        mockRequest 对象是在“真实”测试执行 MockService 端点时定义的。

        为了测试代码,我输入了以下测试代码并点击“播放”,直到我对我的覆盖范围感到满意为止。

        然后我发送一个测试步骤来调用 MockService 端点

        代码如下:

        def mockRequestrequestContent = "" 
        if (mockRequest != null) 
            mockRequestrequestContent = mockRequest.requestContent
        else 
            mockRequestrequestContent = "<testRequestXmlOrJson/>" 
        log.info(mockRequestrequestContent) 
        
        //begin script like @sinnerinc's above
        

        注意:当前版本的 SoapUI 5.50 底部没有日志窗口,尝试收集信息具有挑战性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-18
          • 1970-01-01
          • 2016-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多