Friday, July 25, 2014

Web Service Testing Using Mock Service in SoapUI.



In This post, I am going to describe you on the very most useful features of SOAPUI, i.e. “Mock Services”.  
First, we should know what “Mock Service” is.
Here Mocking means verifying or testing the actual functionality of a product before testing the product on real service.

In short, when we do not have access to the real web service or server, then simulating or creating stub or local service to check the actual functionality of the service is working fine.

To mock the service please follow the steps,

In this post, I will describe the different ways of testing the service using Mock Service features of SoapUI.
There are two types of Mock Services,
1.      Static Mock Response.
2.      Dynamic Mock Response.

In Static Mock Response, we will get only one kind of response,
In Dynamic Mock Response, Different Mock Response for each or different request receive. Just like live service.

Now let us start with creating Mock Service in SoapUI. Here for my post purpose I am using ‘geoipservice’ (http://www.webservicex.net/geoipservice.asmx?WSDL).

By using above WSDL, create Project as described in my previous post (http://lgsofttest.blogspot.no/2013/12/basics-of-webservice-testing-tool-soapui.html) once you create project, in SoapUI it looks likes as shown in below,

 Now expand the service ‘GetIPServiceSoap’, there you will find the Request or Operations, and create test suite and test case for the request “GetGeoIP”. Please refer my previous blog (http://lgsofttest.blogspot.no/2013/12/basics-of-webservice-testing-tool-soapui.html) to create the Test Suite, Test Case and how to add Test Steps.

In addition, add Data Source and Data Source Loop steps to the Test Step.
Once you done with creating Test Suite and Test Case along with Request in Test Step, the project structure looks like below.


Now it is time to add our request to Mock Service, select the request of the “GetGeoIP”, right click on it, and select ‘Add to MockService’, as below.





















When you select the ‘Add to MockService’ then you will one smaller window, which ask for the name that you want to give it to the mock service. Here I will rename the service as “Test Mock Service” and Click on ‘OK’ button, then SoapUI will add request to Mock Service with default Response attached to it, once you done with it and looks like below;

OR
In a case where we do not have, any default response then have to add our own response valid xml format.
For the post purpose, here I will use the default response.

Now most important thing to do is, creating Test data for our request, in our request there is only one field is present, where need to enter the IP address, along with this need to Response data to the same file (or you can use different file as per your requirements).

Here I will add Test data and response data into the same file and finally the excel file looks like below,

Import this excel file into the Data Source in the Test Step, Refer my previous blog (http://lgsofttest.blogspot.no/2013/04/datadriven-testing-from-soapui-using.html) for how to import excel file to Data Source and mapping the fields.

Once importing the excel file and mapping to fields is done then Data Source and our Request looks like below;

DataSource:-
Request:-
Now it is time to move on to the Mock Service section,

Open the Mock Service that we have created above with the name as ‘Test Mock Service’. Open it by double click or Right Click and select ‘Show MockService Editor’. Mock Service editor opens there you will see the Request Name as ‘GetGeoIP’; this is the request which we have added to Mock Service.
It is time to start the Mock Service, by clicking the ‘Start’ button, but before that, we need to configure the Mock Response. To do that Click on the Request, which is present under Mock Service editor, now you will see Response 1 in GetGeoIP request.


Option I :- Static Mock Response
Provide the values as per your requirement in the default response xml. Here I have provided or configured the Mock Response or Default Response xml with some sample values and it looks like below,

Now we all set to test our service/Request using Mock Service with Mock Response.

Click on Setting buttonto configure the Mock Service details, such as Port Number, Host Name and other stuffs as per your needs, please find the below screen shot which I have configured the mock service. Once configuration is done Click on OK.


Click thebutton to start the Mock Server/Service. The Mock Service will run on the Host and Port on which you provided in settings.
Once you start the mock service, you can see the service running status in Mock Service Editor bar as below,


Once you confirmed that the Mock Service is running, and then go to Test Suite where request is present open the request and add the new endpoint as same as which you configured in Mock Service settings box. This New endpoint you can get from browser address bar when you click the button in Mock Service editor (as shown in above screenshot.), follow the steps to add new endpoint or server address as below,


Once you select the Option as “add new endpoint” one-popup box opens, there need to enter the new endpoint and click ok, new endpoint will looks like below and its visible in address bar;


Make sure the Mock Service is running , and now Click thebutton to Run the request, once the request executed successfully in right hand side pane you will see the response which is same as you configured in Mock Services section.

Open the Test Case Editor to execute all your test cases, in the Right hand side pane of the Test Case editor you can see test steps arranged as Data Source, Test Request and Data Source loop.


Click thebutton to run the Test Case, SoapUI will run all the test case, which are present in Data Source until last row.

Once the Test case execution is done you will see the status as “Finished” and in below pane you will see the logs how the test case executed in order, click on the Request in logs pane to see the request and response xmls.

Click on the any “GetGeoIP - Request 1” step in logs pane, a new window opens there you will see Request and Response tabs, click on respected tab to view the details,

Below is the Request XML, which sent with IPAddress,

Response xml, which got from Service/Server, here Server/Service, is our own created Mock Service; the response is same for all the test case, as we have created Static Mock response as below,



Yes, now you are done with Testing of service with use of Mock Service, Here I have done Mock Service testing of service for static values in Mock Response, in my next post I will describe about Dynamic Mock Response.

Please note :-  This is for SoapUI Pro version not SoapUI Open source, It can be done with SoapUI Open Source also only thing is in SoapUI open source there is no Data Source and Data Source Loop feature available. Other than Data Source and Data Source Loop, Adding Request to the Mock Service, configuring the Mock Response with our own values and changing the Mock Service sever settings like Port and Host name is available in SoapUI open source.

So In SoapUI Open Source add request to Mock Service and configure settings and Mock Response, start the mock service/server and change the End Point in request address bar and Run the request, you will see the mock response.
With This…

That’s all Folks.


2 comments:

  1. Hi Sunnyboy, nice post can you please share the installation part of SoapUI i.e Download and Install SoapUI

    ReplyDelete
  2. nyc informative article.thanks

    --anshul

    ReplyDelete