Problem
Sometimes you have to test an application that depends on several web services. Imagine that some of those web services fail or change while you are testing your application.
Something like that could difficult your work, that why you may need to fabricate a ad-hoc web service that imitates (in a very simple way) the one that you are invoking.
These fabricated web services are called mock services. Let us see how produce and export one (into a Tomcat Server) using SOAPUI.
1. Create a SOAPUI project from a WSDL
We will use the following wsdl :
http://www.webservicex.com/globalweather.asmx?WSDL
(with the actual .wsdl file the process will be very similar)
So first go to File | New SOAP Project
2. Create a Mock WS from our project
Now once you have the two interfaces, right click on the project and select Generate Mock Service. There you will choose the endpoint and the port. This port will only work for the SOAPUI mock service, if we export the project to a war and deploy it in a tomcat, the port that we will need will be the one in the Tomcat server..
Now we have the mock created. To add mock responses we click on it and start inventing responses using the template that it offers us. For instance I added two dummy ones. This web service will give you only two responses and it will give them to you sequentially (independently from the input)
You can test your own mock ws by clicking on the mock and selecting "start minimized",
it will appear below
and it will listen the endpoint
http://localhost:8088/mockGlobalWeatherSoap
(this endpoint will work only for this step, as I said once we deploy it in a tomcat this will change). You can test it and see how it only gives us the two requests that we gave it:
3. Export the mock Service into a .war file
Click the project with the right button and select Deploy Project as WAR
4. Deploy the .war file into a tomcat server
You can download the last version of Tomcat here
If you open the tomcat main folder, you will find this:
Have in mind that to run a tomcat server you will need to have the JAVA_HOME variable set (with java 7 at least if you use Tomcat 8). If you are in a linux machine you will have to change the permissions of the whole bin folder using chmod -R -777 /path-to-bin-folder/.
You should see something like this if everything goes well
Your server now will be listening to the url:
http://localhost:8080/mock/
and the endpoint (for soap requests)
http://localhost:8080/mock/mockGlobalWeatherSoap
You can see it using a browser:
And you can test it using SOAPUI. As you see it gives us the two dummy responses in sequence:
Using the browser you can see the logs, which is a nice feature.