SoapUI Groovy 脚本

发布于 2024-08-11 20:46:24 字数 675 浏览 5 评论 0 原文

我正在尝试读取传入的请求 &根据 soapUI 3.0 中请求中传入的值设置模拟响应。我为此使用以下常规脚本。

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:无法在 null 对象上调用方法 getContentElement()

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月松间行 2024-08-18 20:46:24

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

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
未蓝澄海的烟 2024-08-18 20:46:24

再说一遍,我很欣赏这是旧的,但是 Sinnerinc 上面的答案并没有解决原来的问题,因为他的解决方案仍然会受到 NPE 的影响,因为 mockRequest 为空。

我有一个相关问题,发现 这篇文章 建议如果模拟服务从未提供过请求并且您单击绿色三角形按钮来运行,则模拟响应将为空剧本!

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!

遗弃M 2024-08-18 20:46:24

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

当“真实”测试执行 MockService 端点时,定义了 mockRequest 对象。

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

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

代码如下:

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

//begin script like @sinnerinc's above

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文