Site icon NodeXperts

API Automation Using Rest -assured With Live Example

What is rest assured?

  1. Rest -assured is a java library that provides multiple classes, interfaces, and methods by which we can test a RESTful API.
  2. We can handle all HTTP requests such as GET, POST, PUT, DELETE, OPTIONS, etc, and validate the response also.
  3. For assertion, it allows us to use TestNG, Junit, and AssertJ testing libraries, etc.
  4. It is the best choice for API Automation because it easily integrates with Cucumber, Serenity, Logger, and other additional libraries which makes the process easy.

Requirements:

  1. Java (Version >= 8)
  2. IntelliJ IDEA (I’m using this IDE but we can use other IDE also such as Eclipse, NetBeans, etc)
  3. POSTMAN Tool
  4. Dependencies:  Rest Assured, Cucumber, SLF4J Logger, JUnit, GSON, Or Jackson Databind Library

 

Live Example:

Here, I’m going to automate Shopify order creation API with the help of rest assured and other additional libraries which I have mentioned above in the requirements.

 

Step 1: Test the desired API using POSTMAN before starting automation.

Select the HTTP method “POST” and enter this request URL 

JSON:
{
“order”: {
“line_items”: [
{
“variant_id”: 41198603108501,
“quantity”: 10
}
]
}
}

Step 2 Create Maven Project in your IDE 

Step 3: Install all needed dependencies using pom.xml in your project.

Rest-Assured:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
</dependency

Cucumber:
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-cucumber</artifactId>
<version>1.9.51</version>
</dependency>

Logger:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha4</version>
</dependency>

GSON:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>

JUnit:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

Step 4: Setup your project directory for managing the files

Step 5: Create the feature file and write the test scenario and steps

Example

Feature: Allow user to create order on Shopify store

As a user,
I want to create an order on my Shopify store

Scenario: To check that user is able to create order on his store with credentials
When User enters valid order details
| variant_id | quantity |
| 41198603108501 | 10 |
And User makes request to create order with order details
Then Order should be created on his shopify store

Step 6: Create a step file that covers all the steps and scenarios

 

Step 7: Create model files for managing request and response

 

OrderCreationRequestModel.java

Order.java

 

Line_items.java

 

Note: In auto-generated POJO, the “Integer” value can be considered as “String” so please update datatype accordingly in the project.

OrderCreationResponseModel.java

 

 

Order.java

Line_items.java

Note: Many response POJO files will be generated but I have taken only important files.

Step 8: Write the code in step file and run the scenarios from feature file

package demo.project.step;
import com.google.gson.Gson;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import demo.project.model.orderCreationModel.request.Line_items;
import demo.project.model.orderCreationModel.request.Order;
import demo.project.model.orderCreationModel.request.OrderCreationRequestModel;
import demo.project.model.orderCreationModel.response.OrderCreationResponseModel;
import org.junit.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;

public class orderCreationOnShopifyStore {
private final Logger log = LoggerFactory.getLogger(“Logger”);
private final Gson gson = new Gson();
private OrderCreationRequestModel orderCreationRequestModel = new OrderCreationRequestModel();
private OrderCreationResponseModel orderCreationResponseModel = new OrderCreationResponseModel();
private Order order = new Order();
private RequestSpecification httpRequest = RestAssured.given();
private Response jsonResponse;

@When(“^User enters valid order details$”)
public void userEntersValidOrderDetails(List<Line_items> line_itemsList) {
if (line_itemsList != null) {
order.setLine_items(line_itemsList);
orderCreationRequestModel.setOrder(order);
}
}

@And(“^User makes request to create order with order details$”)
public void userMakesRequestToCreateOrderWithOrderDetails() {
httpRequest
.header(“Content-Type”, “application/json”)
.header(“X-Shopify-Access-Token”, “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”);
jsonResponse = httpRequest
.body(gson.toJson(orderCreationRequestModel))
.post(“https://litmusbox-6.myshopify.com/admin/api/2021-07/orders.json”);
}

@Then(“^Order should be created on his shopify store$”)
public void orderShouldBeCreatedOnHisShopifyStore() {
try {
Assert.assertTrue(jsonResponse.getStatusCode() >= 200);
orderCreationResponseModel = gson.fromJson(jsonResponse.prettyPrint(), OrderCreationResponseModel.class);
Assert.assertEquals(orderCreationRequestModel.getOrder().getLine_items().get(0).getVariant_id(), orderCreationResponseModel.getOrder().getLine_items().get(0).getVariant_id());
Assert.assertEquals((int) orderCreationRequestModel.getOrder().getLine_items().get(0).getQuantity(), orderCreationResponseModel.getOrder().getLine_items().get(0).getQuantity());
log.info(“Assertion have been passed successfully.”);
} catch (Exception exception) {
exception.printStackTrace();
log.error(“Exception has been occurred !!!”);
} finally {
orderCreationRequestModel = null;
orderCreationResponseModel = null;
order = null;
httpRequest = null;
jsonResponse = null;
log.info(“Memory has been clean successfully.”);
}
}
}

Conclusion:

Did you find this blog post helpful? If yes then please like it and share it.