OnRequest选项卡soapUI错误

时间:2014-04-18 16:34:02

标签: java groovy soapui

在soapUI模拟请求的OnRequest脚本选项卡上运行此groovy脚本时,我收到以下错误

mockRunner.returnFile(mockRequest.httpResponse, new File(projectDir,"xmlresponse.xml")) return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)

    ERROR:An error occurred [groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.mock.WsdlMockRunner.returnFile() is applicable for argument types: (org.mortbay.jetty.Response, java.io.File) values: [HTTP/1.1 200 

, C:\UXX\XXXX\AAAAA\xmlresponse.xml]], see error log for details

感谢任何帮助。谢谢。

编辑:

我尝试了你的建议,但收到了这个错误。 You cannot create an instance from the abstract interface.然后我尝试了另一种方式,通过搜索网络找到了。

def response = mockRequest.httpResponse
File file = new File("C:/Users/xx/xx/xxx/xmlresponse.xml")
FileInputStream fin = new FileInputStream(file);
response.setStatus( HttpServletResponse.SC_OK )
long length = file.length();
response.setContentType("application/xml;charset=utf-8" );
Tools.readAndWrite( fin, length, response.getOutputStream() );
fin.close();
return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest) 

但问题是我在响应端的Json和raw选项卡中获取xml响应。当我在上面的代码中将内容设置为像此response.setContentType("application/xml;charset=utf-8" )这样的xml时,它不会在json和xml选项卡中提供任何内容,但会在raw选项卡中提供包含xml内容的响应。如何在xml选项卡中返回xml内容?

2 个答案:

答案 0 :(得分:1)

com.eviware.soapui.impl.wsdl.mock.WsdlMockRunner.returnFile()函数接受

javax.servlet.http.HttpServletResponse

and File

而不是javax.servlet.http.HttpServletResponse您发送org.mortbay.jetty.Response。要发送javax.servlet.http.HttpServletResponse值,您可以执行以下操作..

import javax.servlet.http.HttpServletResponse;

def response = new HttpServletResponse();

response.setStatus(200)
response.setContentType("text/xml;charset=UTF-8");

链接:Values not passing from TextBox to Servlet File

然后将响应变量传递给returnFile()函数。

注意:代码未经测试!

答案 1 :(得分:0)

我在Soap UI 5中的模拟REST请求/响应时遇到了完全相同的问题

另一个答案在理论上是正确的,在groovy脚本中创建响应确实有效。但是,Groovy OnRequest脚本存在一些问题。 以下是我用于创建有效XML响应的代码段(由DivxYoda提供HERE提供):

com.eviware.soapui.impl.wsdl.mock.WsdlMockResult mockResult = new com.eviware.soapui.impl.wsdl.mock.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

经过测试,对我很有用!