SoapUI模拟soap接口返回不同响应(通过groovy脚本)
一、创建soap项目,输入wsdl文件,然后生成SOAP Mock Service,再生成测试用例,然后新建新的响应
WSDL文件:MathUtil.wsdl
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <wsdl:definitions targetNamespace="http://sean.com" 3 xmlns:apachesoap="http://xml.apache.org/xml-soap" 4 xmlns:impl="http://sean.com" 5 xmlns:intf="http://sean.com" 6 xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 7 xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" 8 xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 9 <wsdl:types> 10 <schema elementFormDefault="qualified" 11 targetNamespace="http://sean.com" 12 xmlns="http://www.w3.org/2001/XMLSchema"> 13 <element name="add"> 14 <complexType> 15 <sequence> 16 <element name="x" type="xsd:int"/> 17 <element name="y" type="xsd:int"/> 18 </sequence> 19 </complexType> 20 </element> 21 <element name="addResponse"> 22 <complexType> 23 <sequence> 24 <element name="addReturn" type="xsd:int"/> 25 </sequence> 26 </complexType> 27 </element> 28 </schema> 29 </wsdl:types> 30 <wsdl:message name="addResponse"> 31 <wsdl:part element="impl:addResponse" name="parameters"> 32 </wsdl:part> 33 </wsdl:message> 34 <wsdl:message name="addRequest"> 35 <wsdl:part element="impl:add" name="parameters"> 36 </wsdl:part> 37 </wsdl:message> 38 <wsdl:portType name="MathUtil"> 39 <wsdl:operation name="add"> 40 <wsdl:input message="impl:addRequest" name="addRequest"> 41 </wsdl:input> 42 <wsdl:output message="impl:addResponse" name="addResponse"> 43 </wsdl:output> 44 </wsdl:operation> 45 </wsdl:portType> 46 <wsdl:binding name="MathUtilSoapBinding" type="impl:MathUtil"> 47 <wsdlsoap:binding style="document" 48 transport="http://schemas.xmlsoap.org/soap/http"/> 49 <wsdl:operation name="add"> 50 <wsdlsoap:operation soapAction=""/> 51 <wsdl:input name="addRequest"> 52 <wsdlsoap:body use="literal"/> 53 </wsdl:input> 54 <wsdl:output name="addResponse"> 55 <wsdlsoap:body use="literal"/> 56 </wsdl:output> 57 </wsdl:operation> 58 </wsdl:binding> 59 <wsdl:service name="MathUtilService"> 60 <wsdl:port binding="impl:MathUtilSoapBinding" name="MathUtil"> 61 <wsdlsoap:address 62 location="http://localhost:8080/webservice_create/services/MathUtil"/> 63 </wsdl:port> 64 </wsdl:service> 65 </wsdl:definitions>
最后双击add打开页面写入脚本
脚本如下
1 def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) 2 def holder = groovyUtils.getXmlHolder( mockRequest.requestContent ) 3 4 def x=holder.getNodeValue("//sean:x") 5 def y=holder.getNodeValue("//sean:y") 6 7 if (x=='1' && y=='2') //注意需要加单引号‘’ 8 return"Response 1" 9 else 10 return"Response 2"
response 1对应如下响应
response 2对应如下响应
验证一下,参数为x=1,y=2,可以看出返回的是response1
参数为x=2,y=2,可以看出返回的是response2