【问题标题】:Mock Rest-Service Response (json and xml), using soapui使用 soapui 模拟 Rest-Service 响应(json 和 xml)
【发布时间】:2014-02-18 10:03:41
【问题描述】:

我很困惑,因为我是嘲笑休息服务的初学者。

我正在使用 soapui 来模拟休息服务。为此,我需要编写一个脚本(OnRequest Script)来模拟对我的请求的响应。

所以,我想模拟 2 种类型的响应:application/json 和 application/xml。但我不想阅读任何响应文件。我只想在脚本中写响应(OnRequest Script,in soapui)。

我通过读取响应文件找到了一种方法。在我的情况下,我不需要读取任何文件,而是将响应写入脚本(OnRequest 脚本,在 soapui 中)。

有人说我可以这样使用 groovy:

if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
mockRunner.returnFile(httpResponse,new File(path + "test.xml"))
    log.info "Response returning for Content-Type application/xml"
    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)    
}

但该示例确实读取了一个文件,我想使用 groovy 但只在 groovy 脚本中编写答案。

【问题讨论】:

  • 你好,我终于找到解决方法了。

标签: json rest groovy mocking soapui


【解决方案1】:

我终于找到了解决方法:

if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
    WsdlMockResult mockResult = new WsdlMockResult(mockRequest)
    def httpResponse = mockRequest.httpResponse
    httpResponse.setContentType("application/xml;charset=utf-8")
    httpResponse.writer << "<root><user>ABC</user></root>"
    httpResponse.status = 200
    return mockResult
}

如果您需要,请享受此解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    相关资源
    最近更新 更多