API Testing – Mocking the Web Services

Share on facebook
Share on twitter
Share on linkedin
Application Programming Interface which enables communication and data exchange between two different software; the process of Testing this data exchange communication in the form of request and response by using a tool is called as API Testing.

API – Application Programming Interface which enables communication and data exchange between two different software; the process of Testing this data exchange communication in the form of request and response by using a tool is called as API Testing.

API Testing

Fig: API testing done at Business Layer

In order to test an API, we need a tool. There are many tools available in the market and among them the most commonly used tool is SOAP UI tool. Before moving into testing API service, we must make sure that the Environment is set up properly and the corresponding Database and server connections are configured correctly.

Here I am using SOAP UI tool

To Download and Install the latest version of SOAP UI tool and configure it according to the need

Reference Links

To Install Tool: https://www.soapui.org/

http://www.guru99.com/soapui-tutorial.html

 

 

Mocking:

Stimulation or Virtualization of a dummy Webservice – Using dummy webservice in the place of original webservice

There might occur in a situation where the Web Service is not in our control or the service is not available. So, we use an approach of virtualization for testing purpose—this is known as mocking

Example: what and when to go for Mock Service

Let us consider there is a complex environment that consists of three modules named as ‘A’, ‘B’, and ‘C’. Now A’s response is used as an input for B and B’s response is used as an input for C. Only A and C modules are ready for testing but due to the interdependencies of the modules testing cannot be done. To overcome this, we use mocking a process in which a virtual WSDL file is used to test at the early stage and later when the Module B is ready, then the End to End testing can be performed

The Mocking can be done in two ways; one is using the WSDL file which is not hosted or just mock the request

There are two types of mocking responses:

  • Static
  • Dynamic

How to Mock a Service in SOAP UI Tool:

  • Generate mock service/Create a New Mock Service
  • Add any mock operation that is needed
  • Set the default output value at the response under the added mock operation
  • Run the mock service
  • Test the configured Mock service by running the Request

Static Mocking:

It is used mainly to test the positive scenario. It is the process in which only one default value should be returned as output in the response type; whatever request is sent; it will return only the default value.

Below is an example for configuring the Static Mock Service

Step 1:

Create a Mock service by right clicking the Project Name and selecting the Generate SOAP Mock service

API Testing

The created Mock service consists of the following Response operations as per the selection during the creation process

 

API Testing

Step 2:

Let us now try the Mock Service for the getEmployeeDetails

Double click the getEmployeeDetails operation in the created Mock project

Mock Response is displayed

API Testing

Step 3:

Double click and open the response content to hardcode the needed value in the response

API Testing

List of data that are available in the Database

API Testing

The Response with the hardcoded value that does not exist in the table

Note: we can also hardcode the value by using a variable ${Variable Name} that is declared in the script

Step 4:

Run the getEmployeeDetail mock service

API Testing

It is running on port 8088; so, let’s Run the Request to test the created Mock Service

Running the Request to get the details of Employee -Keerthana

API Testing

Note: Make sure the end point URL matches with the Mock Service running Port

As we have created a Mock Service with a constant value to be returned. The output of the Request returns the response value that is set while creating the Mock

Message Log:

All the request received by the Mock Services are logged in the main window at the bottom

By double clicking the log lines, we can see the details of the message

 

Jetty Log:

As soon as the Mock Service is started, immediately inside soap UI we can see those in the Jetty log at the bottom of the SOAP UI window

 

The same above steps should be followed for triggering Dynamic Response except the responses should be Created and configured before running the mock service and the request

Dynamic Mocking:

Based upon the request value, the response will be triggered from the set of responses that are framed.

 

Based on the value specified in the Dispatch field, the operation is done

Sequence

Irrespective of the data sent in the request, the responses are received in a sequence as how it is defined in the mock response

Run the Mock Service and run the Request from the project folder

Query Match

Based on the query that is configured, the response is triggered for the request that is sent

The Expected value and the Dispatch to should Match to get the correct response or else it will consider the value in the Default Response field and retrieves the response based on that value

Run the Mock Service and run the Request from the project folder

 

Random

Randomly triggers its own Responses when the Request is sent

Run the Mock Service and run the Request from the project folder

Conclusion:

In today’s software world with rapid use of Agile methodology, Mocking services would be of great use since the testers need to work hand in hand with the developers and testers do not need to wait until the webservice is built completely.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Exploratory testing

Exploring the application, understanding how each feature works, and based on understanding testing the whole application – It is exploratory testing.
Exploratory testing needs a tester to listen, read, think, and report, effectively and rigorously.

Read Article »

Mobile Responsive Testing

Mobile Responsive Testing is simply a Testing Process performed to make sure that your website or an app is working appropriately on all types of devices.
Whether it an app or website, they need to ensure that they are accessible from any device, anytime, anywhere.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.