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.
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
The created Mock service consists of the following Response operations as per the selection during the creation process
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
Step 3:
Double click and open the response content to hardcode the needed value in the response
List of data that are available in the Database
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
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
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.