Monday, December 8, 2014

Working With Mock Services (Dynamic Mock Services) in SOAPUI

In my last post, we have seen that what is mock service and how to use it. In the previous blog ( I have described about static mock response, meaning only one response for different inputs in request. Now in this post I am going to describe about dynamic mock response, which gives different response for each different input in request.

Let us start, Create project and test suite, and add Request to Mock Service, as described in my previous blog ( The project structure looks like below, we can see here Default mock response also created.

Before going to next step, we need to prepare the Input and output test data. We need input data to feed the request xml and same we need output data to configure the mock response. Below is sample test date I have prepared for this post and it look like below,

Now we need to write some groovy script to configure the Mock Response to do this, follow the steps.

Go to “GetGeoIP” request in Mock Service open it by right click or double click on it, Mock Service request editor opens, it will looks like below,

Now in the above screen we can see Mock response, and Interface and Operation name. In addition, in below pane we can see Dispatch and value as “SEQUENCE”; this is use for passing value or configuring the mock response. Now to configure mock response as per our predefined response values we need to write groovy script, hence select Dispatch value as “SCRIPT”. Once you selected Dispatch as “SCRIPT” and Default Response will be “Response 1”, which is same as mock response, which created when we added our request to mock services. Below is screenshot how it looks like,

As shown in above screenshot, in Script editor need to write groovy script to configure the mock response. Below is the code to configure the mock response xml.

Mock response has five fields looks like below,
Now write groovy script to pass value to these five fields in mock response.

Now let us describe the groovy script, in simple steps I will try to explain the script.

Step 1 :- Import all necessary packages the groovy, groovy Utils and excel packages.
Step 2 :- Create objects groovyutil to read the response xml. Using requestxmlholder read all the tags in the response xml.
Step 3 :- Read the excel file from the path, and by using for loop condition we can iterate the excel file until its last row.
Step 4 :- Now assign each cell value to a variable.
Step 5 :- Set variable to response xml tag, the value is the variable will set the by this statement.

Once script is ready save the project, and now add above created variable to response xml tag as below,

In above screenshot we can see mapping of the variables to respective xml tags in mock response xml.

Now we are done with mock response xml configuration dynamically. The groovy script will assign new value every time when the mock response when called by the services. With this, we will get different value in response with this we can assume our service is working fine.
Now run the Test Suite to check request and response for different input to different output. Import the excel file which we have created above to Data Source, and map the fields to request xml.
Start the mock service on the host and port as configured (Refer my previous blog to configure the mock service with host and port name
Running mock service will look like below,

Below screenshot shows, the request xml is running on the mock service (Mock Service Endpoint in address bar), and value configured from Data source for request xml field.

Now open the Test Case and run the test case,

Once the test case running completed verify the request and response xml, to validate the result.

Click on any of the request to check input data in request xml and output data in response xml.

Request XML with Input data,

Response xml from Mock response which configured by Groovy script,

Finally, we are done with mock service testing for our request.

In my next post, I will describe another way of Dynamic mock service.

                             That’s all Folks.