What is testing?
Software Testing is an important aspect of delivering a shippable product or service to the market. To ensure the quality, there are various stages where testing is done. Unit Testing – mainly carried out by developers, Functional, Integration, Regression testing (Manual and Automation) mainly carried out by Quality Assurance team, Performance Testing by Performance Engineers. However, there is one more type of testing that lies in between white box testing (Unit Testing) and black box testing (functional testing), that is, grey box testing. In grey box testing we can test the API that is communicating between two applications or even between application and the database. This is a very important aspect, as we can test all the conditions without going into the application and ensuring a bug free product/service.
What is API Testing?
API stands for Application Programming Interface. APIs are mechanisms that enable two software components to communicate with each other. For example, Uber uses google map to show the driver’s location. API testing is a part of grey box testing or functional testing.
Advantages of API Testing?
Earlier Validation
Easier Test Maintenance
Faster Time to Resolution
Speed and Coverage
What tools can be used for testing API?
Postman
RestAssured
GraphQL
What is RestAssured?
RestAssured is a Java-based library for verifying/testing RESTful APIs. The libraries based on the RestAssured library are also capable of validating the HTTP responses from the server. Response status code, body, message, headers, and so on can be tested with the RestAssured library.
Advantages of RestAssured:
It is an open-source Java library
The initial setup is straightforward
It follows the BDD approach and uses keywords like given() when(), and then(), which make the code readable and support clean coding
It has inbuilt methods for the quick assertion of status code, response time, etc.
Headers, cookies, Content-Type, and other request parameters can be verified on the fly
Very rich in the ready-made assertion
The response is given in JSON or XML, so it is easy to parse and validate
It can be easily integrated with testing frameworks like JUnit, TestNG, etc.
Support JSONPath and XMLPath to parse the JSON or XML response
Disadvantages of RestAssured:
RestAssured does not support the testing of SOAP (Simple Object Access Protocol) APIs explicitly
Requires good Java programming knowledge
What dependencies do we need to add?
The following dependencies are added to Pom.xml to perform API testing:
What structure does RestAssured use?
RestAssured structure is very similar to Gherkins. It is divided into 3 parts
Given: It is the pre-condition for your test case
When: here you send the http request to the server
Then: here you will check the response from the server and compare it with what is expected.
RestAssured framework:
Step 1: Create maven project
Step 2: Add dependencies to the pom.xml
Step 3: Under src/test/java we will create different packages like endpoints, payload, test and utilities.
api.endpoints: here we will create different classes like Routes where all url is maintained and endpoints where CRUD methods are implemented
Payload: here we maintain pojoclass of different module where we will extract get and set method and expected Json
Test: here we maintain all the test cases
Utilities: here all utility classes and function are mentioned (all reusable properties)
Step 4: Under src/test/resources we will create a package property where we can mention url, login credentials.
Step 5: create a new folder report where all reports will be maintained.
What are various response status Code in RestAssured?
HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes:
Informational responses (100 – 199)
Successful responses (200 – 299)
Redirection messages (300 – 399)
Client error responses (400 – 499)
Server error responses (500 – 599)
What command does RestAssured have?
With RestAssured 4 basic operations Create, Read, Update, and Delete (CRUD) are performed via the POST, GET, and PUT methods.
GET: To retrieve or read the server’s data.
POST: It is used to add a resource to the server.
PUT: To update an existing record completely.
PATCH: If only a partial update must be made to an existing record.
DELETE: Deletes a resource from a server.
Good job I liked it.
This is great information.