The SOAP Service Mocking functionality in soapUI lets you create a standards compliant simulation of a WSDL based service just from its WSDL contract, which in soapUI is called a “MockService”. This can be run either directly from inside soapUI, with the included command-line runner, or even with a standard servlet container.
The business driver:
Companies may not have message routing infrastructure and the mock proxy provides the poor-man’s alternative!Your company may have multiple non-production environments but the connectivity to 3rd party partners are limited to specific environments only. You can switch the environment internally without impacting connectivity to 3rd party.
Service Mocking is easy- you get the wsdl and import into a SoapUi project and add a Mock Service! It takes little effort to turn the mock service as dynamic proxy and run it as service (unattended).
Here is an example of Mock Service Project and pay attention to Interface, Operation and Request object hierarchy-
Configure the Request(interface–>operation–>request) URL:
In order to turn the mock service as dynamic proxy you will need to use Groovy Scripts and here is the activity flow:
You will need object reference to request via project (from mockOperation)–>interface–>operation–>request.
Set the RequestContent property on the Request object from mockRequest.
Submit the request and get the response.
Finally, you set the ResponseContent property of the Response object from response received at the WsdlSubmit.
Groovy Script that makes the proxy dynamic:
// import all the namespaces to trim the lines of codes import com.eviware.soapui.impl.wsdl.WsdlProject import com.eviware.soapui.impl.wsdl.WsdlInterface import com.eviware.soapui.impl.wsdl.WsdlRequest import com.eviware.soapui.impl.wsdl.WsdlSubmitContext import com.eviware.soapui.impl.wsdl.WsdlSubmit import com.eviware.soapui.model.iface.Response import com.eviware.soapui.model.mock.MockResponse // get reference to project WsdlProject project = (WsdlProject)mockOperation.mockService.project // get reference to request WsdlRequest request = (WsdlRequest)project.interfaces["PartnerInterface"].operations["getResponse"].getRequestByName("Request 1") // set request content from incoming mockRequest request.setRequestContent(mockRequest.getRequestContent()) // submit request asynchronously WsdlSubmit submit=request.submit( new WsdlSubmitContext( request ), false ) // wait for the response Response response = submit.getResponse(); // get reference to MockResponse MockResponse mockResponse=mockOperation.getMockResponseByName("Response 1") // set the mock response content from response received by the request. mockResponse.setResponseContent(response.getContentAsString())
Ok, you are done with the mock service setup and configuration but how do you run it unattended? SoapUI comes with mockservicerunner.bat and you can use this utility to run the project. My attempt to run the mock service as Windows Service failed. Service starts fine with -b (background) option and I can see the port is open via telnet (or netstat) but the service is not accessible. However, the mock service starts fine with the same commands if you start interactively. So, I switched to Windows Scheduler and configured it to behave like a Windows Service.
Windows Scheduler to run mock service unattended:
Use the following at the Run text box of the scheduler-
C:\[your-path]\mockservicerunner.bat -m"Partner MockService" -s"C:\[your-soapui-setting-path]\soapui-settings.xml" "C:\[your-soapui-project-path]\SoapUIMockProxy-soapui-project.xml"
Run As:Set the account that has access to the folders mentioned at the run command.
Check Enabled (scheduled task runs at specif time)
Set the scheduler to start at specif time and run daily.
Stop the task if it runs for 23 hours and 59 minutes.
What that mean is- your scheduler will kick start the mock service at a specific time (12 AM in this example), run for 23 hours 59 seconds and restart it automatically leaving a minute of outage. This scheduler approach is not perfect but it provides you an alternative until SoapUI fix the issue with mock service running under Windows Service.
In my example, I was exposing the mock service over SSL and non-SSL. SSL related settings are stored in soapui-settings.xml. I am not illustrating the usages of SSL to keep this posting simple but you can follow the links below to implement SSL on the mock service.
Acknowledgements:
http://www.soapui.org/apidocs/index.html?com/eviware/soapui/impl/wsdl/submit/transports/http/support/attachments/package-tree.html
http://www.soapui.org/Service-Mocking/mocking-soap-services.html
Using SSL in a SoapUI mock service (including client authentication)