Automated API testing has become an essential part of the software development lifecycle. It helps ensure the functionality, reliability, and performance of APIs. RestAssured, TestNG, Maven, and Extent Report are popular tools used for automation API testing frameworks.
RestAssured is a Java-based library that provides a domain-specific language (DSL) for writing API tests. It simplifies the process of sending HTTP requests and validating responses. With RestAssured, we can easily set headers, query parameters, and request bodies, as well as extract and assert response data.
Steps to Create a REST Assured Framework:
1. Create a Maven Project in the Eclipse/IntelliJ or other IDE ( Here we are using Intellij Idea IDE )
File->New-> Project -> Maven -> Maven Project -> Create a simple Project -> Next -> Enter Group id ->Artifact id-> Finish
- Add necessary dependencies and plugins in the maven pom.xml file. ( Here are list )
- Rest Assured (io.rest-assured:rest-assured): Essential for simplified REST API testing in Java projects.
- TestNG (org.testng:testng): A versatile testing framework for Java, aiding in test organization and execution.
- Extent Reports (com.aventstack:extentreports): Used to generate detailed and interactive test reports in Java test automation projects.
- JSON Path (io.rest-assured:json-path): Facilitates the extraction and manipulation of JSON data in REST API responses.
- JSON Schema Validator (com.github.fge:json-schema-validator): Allows validation of JSON responses against predefined JSON schemas, ensuring data conformity.
- JavaFaker (com.github.javafaker:javafaker): A tool for generating fake data, often used for test data creation in Java projects.
- JSONAssert (org.skyscreamer:jsonassert): A library for making assertions on JSON responses, useful for comprehensive response validation.
and in addition to that we added the
maven-compiler-plugin to specify Java compilation settings and the
maven-surefire-plugin to run TestNG tests based on the
testng.xml configuration. This configuration ensures that your TestNG tests are executed and reported as part of your Maven build process, making test execution efficient and automatable.
Automate HTTP Request:
REST Assured simplifies API testing by using a Behavior-Driven Development (BDD) format. This approach breaks down HTTP requests into three parts:
- given(): This is where you provide input details such as the Base URI, Headers, Path Parameters, Query Parameters, and Request Body or Payload.
- when(): Here, you specify the resource you want to interact with and the HTTP request method you intend to use, whether it’s POST, GET, PUT, PATCH, or DELETE.
- then(): This section focuses on validating the response, including elements like the response code, response time, response message, response headers, response body, and more.
Using these given-when-then statements, REST Assured streamlines the process of automating HTTP requests and makes API testing more intuitive and effective.
Framework Structure Explanation:
- [ ] Routes: The “Routes” class in this RESTAssured framework provides URL paths for API endpoints, enabling interactions with user accounts and a bookstore’s books
- [ ] UserEndPoints & BookStoreEndpoints : The “UserEndPoints” & “BookStoreEndPoints” class in this RESTAssured framework provides generic methods for making HTTP requests, such as POST and DELETE, with appropriate headers and authentication for user-related operations in the API.
- [ ] DataProviders : The “DataProviders” class contains various TestNG data providers for supplying test data to API test cases
- [ ] RequestBodyBuilder : The “RequestBodyBuilder” class generates JSON request bodies for API calls, and it also contains a sample API response JSON in the “allBookAPIResponse” field
- [ ] ExtentReportManager: The “ExtentReportManager” class is for creating and configuring Extent Reports for test automation.
- [ ] TestNgListener: The “TestNgListener” class is a TestNG listener that creates and configures Extent Reports for detailed test execution reporting. It captures events like test success, failure, and skipped tests, generating corresponding logs with labels and colors. The reports are created at the start of a test suite and flushed at its completion, enhancing reporting capabilities in automation frameworks.
- [ ] BaseTest: In the BaseTest class, we create a new user and authorize them to perform operations. These steps are necessary to make API calls, which generate a token that is used in relevant APIs.
- [ ] test: in this, we added all the API test cases with modules
Running Test Cases:
—> we can use the below command to run test cases that are included in the testng.xml suite.
mvn clean test
Here is an Extent Report:
Here is github Repo link : https://github.com/AppmetryTech/restassured_testng_framework