Approach
1: Convert response data to list and calculate size
Response
response =
given()
.header("Accept",
"application/json")
.when()
.get("api/v1/employees/")
.then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON)
.extract()
.response();
Employee[]
empArray = response.as(Employee[].class);
List<Employee>
emps1 = Arrays.asList(empArray);
Approach
2: Using jsonPath and getList method.
given()
.header("Accept", "application/json")
.when()
.get("api/v1/employees/")
.then()
.extract()
.jsonPath().getList("$").size();
Approach
3: Using body, size()
method.
given()
.header("Accept",
"application/json")
.when()
.get("api/v1/employees/")
.then()
.assertThat().statusCode(200).and()
.contentType(ContentType.JSON).and()
.body("size()", is(size));
package com.sample.app;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.sample.app.model.Employee;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
public class TestApp {
@Test
public void testApp() {
RestAssured.baseURI = "http://localhost:8080";
/* Convert response data to list and calculate size */
Response response = given().header("Accept", "application/json").when().get("api/v1/employees/").then()
.assertThat().statusCode(200).and().contentType(ContentType.JSON).extract().response();
Employee[] empArray = response.as(Employee[].class);
List<Employee> emps1 = Arrays.asList(empArray);
System.out.println("List Size : " + emps1.size());
/* Using jsonPath and getList method */
int size = given().header("Accept", "application/json").when().get("api/v1/employees/").then().extract()
.jsonPath().getList("$").size();
System.out.println("List Size : " + size);
/* You can validate size like below */
given().header("Accept", "application/json").when().get("api/v1/employees/").then().assertThat().statusCode(200)
.and().contentType(ContentType.JSON).and().body("size()", is(size));
}
}
No comments:
Post a Comment