REST Assured
Course Index
Index

REST Assured Basics

1. What is REST Assured, and why is it used?

REST Assured is a Java-based library used for testing RESTful APIs. It simplifies API automation by providing an easy-to-use syntax for sending HTTP requests and validating responses.
Why use REST Assured?

  • Supports GET, POST, PUT, DELETE, PATCH requests
  • Provides built-in assertions for response validation
  • Supports JSON and XML response handling
  • Can be easily integrated with TestNG, JUnit, and CI/CD tools

2. What are RESTful APIs, and how do they work?

A RESTful API (Representational State Transfer API) follows REST principles, allowing clients to communicate with a server using HTTP methods like:

  • GET : Retrieve data
  • POST : Send data (create new resource)
  • PUT : Update a resource
  • PATCH : Modify part of a resource
  • DELETE : Remove a resource
RESTful APIs use JSON/XML for data exchange and follow a stateless architecture.

3. What are the key features of REST Assured?

  • Simple and Readable Syntax – Uses Given-When-Then pattern
  • Supports All HTTP Methods – GET, POST, PUT, DELETE, PATCH
  • Built-in Assertions – Validate status codes, headers, and body
  • JSON & XML Support – Easily parse API responses
  • Authentication Support – Handles Basic Auth, OAuth, JWT
  • Logging Capabilities – Debug requests and responses
  • Integration with TestNG & JUnit – Works with testing frameworks

4. How do you set up REST Assured in a Java project?

Follow these steps:

Add REST Assured Dependency (Maven)


XML
Copy
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.4.0</version>  <!-- Use the latest version -->
</dependency>


Import REST Assured classes


import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*;

Start writing API tests!

5. What is the basic structure of a REST Assured test?

A REST Assured test follows the Given-When-Then format:

Java
Copy
given()
    .baseUri("http://192.xxx.xx.xxx:8080")
    .when()
    .get("/books/20250401")
    .then()
    .statusCode(200)
    .body("isbn", equalTo(20250401));

  • Given() : Setup request (headers, auth, parameters)
  • When() : Send the request
  • Then() : Validate the response

6. How do you send a GET request using REST Assured?

Java
Copy
given()
    .when()
    .get("http://192.xxx.xx.xxx:8080/books/20250401")
    .then()
    .statusCode(200)

7. How do you send query parameters in a REST Assured request?

Java
Copy
given()
    .queryParam("isbn", 20250401)
    .when()
    .get("http://192.xxx.xx.xxx:8080/books")
    .then()
    .statusCode(200);

8. How do you send path parameters in a REST Assured request?

Java
Copy
given()
    .pathParam("isbn", 20250401)
    .when()
    .get("http://192.xxx.xx.xxx:8080/books/{isbn}")
    .then()
    .statusCode(200);

9. How do you add headers to a REST Assured request?

Java
Copy
given()
    .header("Content-Type", "application/json")
    .when()
    .get("http://192.xxx.xx.xxx:8080/books")
    .then()
    .statusCode(200);

10. How do you send a POST request with a JSON payload in REST Assured?

Java
Copy
given()
    .contentType("application/json")
    .body("{\"price\": 100.0,\"name\": \"Book 2\",\"publisher\": \"ABC publisher\",\"language\": \"English\"}")
    .when()
    .post("http://192.xxx.xx.xxx:8080/books")
    .then()
    .statusCode(201);

11. How do you send authentication details in REST Assured?

Basic Auth:

Java
Copy
given()
    .auth().basic("username", "password")
    .when()
    .get("https://api.example.com/protected")
    .then()
    .statusCode(200);

12. How do you log requests and responses in REST Assured?

Java
Copy
given()
    .log().all()
    .when()
    .get("http://1xx.xxx.xx.xxx:8080/books/20250401")
    .then()
    .log().all();

13. What is the difference between given(), when(), and then() in REST Assured?

  • given() : Sets up request (headers, auth, body, parameters)
  • when() : Sends the request
  • then() : Validates the response

14. What are some common assertions used in REST Assured?

Java
Copy
// Status Code Validation:
.then().statusCode(200);
// Response Body Validation (JSON):
.then().body("userId", equalTo(1));
// Response Header Validation:
.then().header("Content-Type", equalTo("application/json; charset=utf-8"));
// Response Time Validation:
.then().time(lessThan(2000L));