Загрузка...

How to Validate Complex Json Response Using Rest Assured | Api testing | Qa Automation | SDET

How to Validate Complex JSON Responses Using Rest Assured

When working with REST APIs, validating complex JSON responses is a core task for any SDET. With Rest Assured, you can approach this in three powerful ways:

1. Using JsonPath for Quick Extraction

JsonPath is perfect for accessing deeply nested fields or lists within a response.

Response response = given().get("/api/users").then().extract().response();
String name = response.jsonPath().getString("data.users[0].name");
int totalUsers = response.jsonPath().getList("data.users").size();

Great for quick checks and conditional filters:

List String activeUsers = response.jsonPath()
.getList("data.users.findAll { it.status == 'active' }.name");

2. Inline Validation with Hamcrest Matchers

Rest Assured integrates seamlessly with Hamcrest for readable and fluent assertions:

given().get("/api/users")
.then()
.statusCode(200)
.body("data.users[0].name", equalTo("John"))
.body("data.users.size()", greaterThan(0));

This approach is ideal for chaining multiple assertions in a single test.
3. Deserialize into POJOs for Complex Structures

For large or reusable responses, convert the JSON into Java objects:

UserResponse userData = response.as(UserResponse.class);
assertEquals("John", userData.getUsers().get(0).getName());

This makes validations cleaner, especially when dealing with nested or repeating structures.
---

🚀 Pro Tip:

Use POJOs for structured validation, JsonPath for dynamic filters, and Hamcrest for readable inline assertions. Combine all three based on your test need and response complexity!

#RestAssured #SDET #APITesting #Automation #Java #QA #TestingTips

Видео How to Validate Complex Json Response Using Rest Assured | Api testing | Qa Automation | SDET канала Viplove QA - SDET
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять