In RestAssured, you can check the length of a JSON array response by extracting the array and then using assertions to verify its size. Here's how you can do it:
Assuming you have a JSON response like this:
{ "data": [ {"id": 1, "name": "John"}, {"id": 2, "name": "Alice"}, {"id": 3, "name": "Bob"} ] } You can check the length of the "data" array using RestAssured as follows:
import io.restassured.RestAssured; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class TestClass { public static void main(String[] args) { // Base URI RestAssured.baseURI = "https://api.example.com"; // Send GET request and extract response String response = given() .when() .get("/endpoint") .then() .statusCode(200) .extract() .asString(); // Check the length of the JSON array given() .contentType("application/json") .body(response) .when() .get() .then() .assertThat() .body("data.size()", equalTo(3)); // Adjust the expected size according to your JSON array length } } In this example:
body("data.size()", equalTo(expectedSize)) assertion to check if the size of the "data" array in the JSON response matches the expected size.Replace "https://api.example.com" with your actual base URI and "/endpoint" with the endpoint you are testing. Adjust the expected size (equalTo(3)) according to the length of your JSON array.
RestAssured check JSON array response length
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", equalTo(5)); RestAssured assert JSON array length
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", greaterThan(0)); RestAssured validate JSON array response size
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", lessThanOrEqualTo(10)); How to check JSON array length in RestAssured
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", equalTo(3)); RestAssured verify JSON array size
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", greaterThanOrEqualTo(2)); RestAssured check length of array in JSON response
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", equalTo(4)); Validate JSON array size with RestAssured
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", lessThan(20)); RestAssured assert JSON array size
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", equalTo(6)); RestAssured validate JSON array length
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", greaterThan(1)); RestAssured check size of JSON array response
given(). when(). get("/api/endpoint"). then(). assertThat(). body("data.size()", equalTo(7)); wmi seconds groovy sql-query-store angular2-http println stm32 kramdown compression dart-http