E2E_API_Testing_Flow_in_RestAssured

E2E API Testing Flow in REST Assured

In this article we will see E2E API Testing Flow in REST Assured for REST APIs can be easily acheived. Let’s get right into it!


1. GET Request in REST Assured

A GET request is used to retrieve data from an API.

Basic GET Request

given()
.when()
.get("https://reqres.in/api/users/2")
.then()
.statusCode(200);

Query Parameters

Query parameters are appended to the URL to filter or search data.

Example URL:

https://reqres.in/api/users?page=2

REST Assured:

given()
.queryParam("page", 2)
.when()
.get("https://reqres.in/api/users")
.then()
.statusCode(200);

Path Parameters

Path parameters are part of the URL path.

Example URL:

https://reqres.in/api/users/2
given()
.pathParam("id", 2)
.when()
.get("https://reqres.in/api/users/{id}")
.then()
.statusCode(200);

Multiple Parameters

You can use multiple query parameters together.

given()
.queryParam("page", 2)
.queryParam("limit", 5)
.when()
.get("https://example.com/users");

Dynamic URLs

Store URLs in variables to make tests reusable.

String baseUrl = "https://reqres.in";
given()
.when()
.get(baseUrl + "/api/users/2");

2. POST Request in REST Assured

A POST request is used to create a new resource.


JSON Request Body

String requestBody = """
{
"name": "John",
"job": "QA Engineer"
}
""";

Creating a Resource

given()
.contentType("application/json")
.body(requestBody)
.when()
.post("https://reqres.in/api/users")
.then()
.statusCode(201);

Adding Headers

Headers provide additional information about the request.

given()
.header("Authorization", "Bearer token")
.header("Accept", "application/json")
.body(requestBody)
.when()
.post("https://example.com/api/users");

Content Type

Always specify the content type when sending JSON data.

.contentType("application/json")

3. PUT vs PATCH Request

Both methods update existing resources, but they behave differently.

PUTPATCH
Full updatePartial update
Replaces the entire resourceUpdates only specified fields
Sends all fieldsSends only changed fields

PUT Request Example

String body = """
{
"name":"John",
"job":"Senior QA"
}
""";
given()
.contentType("application/json")
.body(body)
.when()
.put("https://reqres.in/api/users/2")
.then()
.statusCode(200);

Use PUT when replacing the complete resource.


PATCH Request Example

String body = """
{
"job":"Automation Lead"
}
""";
given()
.contentType("application/json")
.body(body)
.when()
.patch("https://reqres.in/api/users/2")
.then()
.statusCode(200);

Use PATCH when updating only specific fields.


4. DELETE Request

A DELETE request removes a resource from the server.

given()
.when()
.delete("https://reqres.in/api/users/2")
.then()
.statusCode(204);

Status Validation

Always validate the response status code.

.then()
.statusCode(204);

Common status codes:

  • 200 – Success
  • 201 – Resource Created
  • 204 – Resource Deleted
  • 400 – Bad Request
  • 404 – Resource Not Found

Cleanup

DELETE requests are commonly used in automation frameworks to remove test data created during execution. This helps keep the test environment clean and prevents unwanted data from affecting future test runs.


Best Practices

  • Use GET to retrieve data.
  • Use POST to create new resources.
  • Use PUT for full updates.
  • Use PATCH for partial updates.
  • Use DELETE to remove resources.
  • Validate response status codes after every request.
  • Reuse common configurations with RequestSpecification.

Conclusion

We achieved E2E API Testing Flow in REST Assured for REST APIs. REST Assured provides a clean and readable way to automate CRUD operations. By understanding how to use GET, POST, PUT, PATCH, and DELETE requests, along with query parameters, path parameters, headers, and JSON payloads, you can build reliable and maintainable API automation tests. These are the core operations you’ll use in almost every REST Assured project.

Related Articles


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply