REST Assured
Course Index
Index

REST Assured Advanced

1. How do you handle dynamic responses in REST Assured?

Java
Copy
// Extract dynamic values using JsonPath:
Response response = given().get("https://api.example.com/users");
String userId = response.jsonPath().getString("users[0].id");

// Use Regex for Partial Matches:
.then().body("name", matchesPattern("John.*"));

2. What is JsonPath in REST Assured, and how is it used?

JsonPath is used to extract values from JSON responses.

Java
Copy
Response response = given().get("https://api.example.com/users/1");
String name = response.jsonPath().getString("name");

3. What is XmlPath in REST Assured, and when do you use it?

XmlPath is used to parse XML responses.

Java
Copy
Response response = given().get("https://api.example.com/data.xml");
String title = response.xmlPath().getString("book.title");

4. How do you validate nested JSON responses using REST Assured?

Example JSON Response:

JSON
Copy
{
  "user": {
    "id": 1,
    "name": "John",
    "address": { "city": "New York" }
  }
}
}


Validating Nested JSON Fields:
Java
Copy
.then().body("user.address.city", equalTo("New York"));

5. What are Hamcrest matchers, and how are they used in REST Assured?

Hamcrest matchers provide flexible assertions:

Java
Copy
.then().body("userId", is(1))
      .body("title", containsString("API"))
      .body("tags", hasItems("java", "rest-assured"));

6. How do you perform schema validation in REST Assured?

Add JSON Schema Validator dependency:

XML
Copy
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>5.4.0</version>
</dependency>


Validate JSON Schema:
Java
Copy
.then().body(matchesJsonSchemaInClasspath("schema.json"));

7. What is a Request Specification, and why is it useful?

  • Request Specification avoids repetition in tests.
  • Reuse in multiple tests.

Java
Copy
RequestSpecification reqSpec = given().baseUri("https://api.example.com")
                                      .header("Content-Type", "application/json");
given().spec(reqSpec).when().get("/users").then().statusCode(200);

8. What is a Response Specification in REST Assured?

  • Response Specification defines common response validations.
  • Reuse it in tests.

Java
Copy
ResponseSpecification resSpec = expect().statusCode(200)
                                       .contentType(ContentType.JSON);
given().spec(reqSpec).when().get("/users").then().spec(resSpec);

9. How do you handle file uploads using REST Assured?

Java
Copy
given().multiPart("file", new File("test.pdf"))
       .when().post("/upload")
       .then().statusCode(200);

10. How do you handle OAuth authentication in REST Assured?

Using Bearer Token:

Java
Copy
given().header("Authorization", "Bearer YOUR_ACCESS_TOKEN")
       .when().get("/secure-data")
       .then().statusCode(200);

11. What are some advanced assertions you can perform in REST Assured?

Java
Copy
// Check response time:
.then().time(lessThan(2000L));

// Validate multiple JSON fields:
.then().body("users[0].name", equalTo("John"))
      .body("users.size()", greaterThan(5));

12. How do you integrate REST Assured with TestNG?

Java
Copy
@Test
public void testAPI() {
    given().when().get("/users").then().statusCode(200);
}

13. How do you implement data-driven testing in REST Assured?

Use TestNG @DataProvider:

Java
Copy
@DataProvider(name = "userData")
public Object[][] getData() {
    return new Object[][] { {"John", 25}, {"Alice", 30} };
}

@Test(dataProvider = "userData")
public void testAPI(String name, int age) {
    given().param("name", name).param("age", age)
           .when().get("/users")
           .then().statusCode(200);
}