我试图使用 soapUI 模拟以下 REST Web 服务
sample 请求:
<Request>
<HistoricTxn>
<reference>E1</reference>
<method>txn</method>
</HistoricTxn>
</Request>
样本响应1
<Response>
<reason>ACCEPTED</reason>
<status>1</status>
<time>10:12</time>
</Response>
样本响应2
<Response>
<information>Failure on invalid request details</information>
<reason>fails Luhn check</reason>
<status>3</status>
<time>10:15</time>
</Response>
通常我使用这种 groovy 脚本来评估请求并输出动态响应。 (使用soapUI模拟SOAP网络服务时)
常规脚本:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());
def reference = holder.getNodeValue("//reference");
if(reference == "Success"){
return "SampleResponse1";
} else {
return "SampleResponse2";
}
不幸的是,当我尝试向此 REST 模拟服务端点发送请求时,它会返回错误。 错误:
com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException: Cannot invoke method getRequestContent() on null object
我了解错误消息显示 getRequestContent() 已返回空值,因此我收到此异常。但同样适用于 SOAP 模拟服务,不会返回空值或导致异常。感谢任何解决此问题的解决方法。
最佳答案
我刚刚发现 mockRequest.getRequestContent() 对于发送到使用 soapUI(版本 5.2.1)模拟的 REST Web 服务的所有 POST、PUT、DELETE 请求返回 null
def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());
由于上面的(RequestContent)返回null,soap UI无法计算下面标签的值。
def reference = holder.getNodeValue("//reference");
SoapUIOfficial References for this bug in soapUI(version 5.2.1):
关于web-services - SoapUI REST 模拟服务在模拟以根据请求值处理动态响应时给出错误(但相同的方法适用于 SOAP 模拟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38313401/