2016-04-07 7 views
5

Ho un endpoint che restituisce un JSON come:RestAssured: come verificare la lunghezza della risposta dell'array json?

[ 
    {"id" : 4, "name" : "Name4"}, 
    {"id" : 5, "name" : "Name5"} 
] 

e una classe DTO:

public class FooDto { 
    public int id; 
    public String name; 
} 

Ora, sto testando la lunghezza della matrice JSON restituita in questo modo:

@Test 
public void test() { 
    FooDto[] foos = RestAssured.get("/foos").as(FooDto[].class); 
    assertThat(foos.length, is(2)); 
} 

Ma, c'è un modo per farlo senza il cast di FooDto array? Qualcosa del genere:

@Test 
public void test() { 
    RestAssured.get("/foos").then().assertThat() 
     .length(2); 
} 

risposta

19

Risolto! L'ho risolto in questo modo:

@Test 
public void test() { 
    RestAssured.get("/foos").then().assertThat() 
     .body("size()", is(2)); 
} 
1

Ci sono modi. Ho risolto con il sottostante

@Test 
public void test() { 
ValidatableResponse response = given().when().get("/foos").then(); 
response.statusCode(200); 
assertThat(response.extract().jsonPath().getList("$").size(), equalTo(2)); 
} 

utilizzando restassured 3.0.0

2

ho risolto simile compito spirito GPath.

Response response = requestSpec 
       .when() 
       .get("/new_lesson") 
       .then() 
       .spec(responseSpec).extract().response(); 

Ora posso estrarre il corpo di risposta as String e utilizzare funzionalità integrate GPath

String responseBodyString = response.getBody().asString(); 

assertThat(from(responseBodyString).getList("$").size()).isEqualTo(YOUR_EXPECTED_SIZE_VALUE); 
assertThat(from(responseBodyString).getList("findAll { it.name == 'Name4' }").size()).isEqualTo(YOUR_EXPECTED_SUB_SIZE_VALUE); 

Ad esempio completo vedi http://olyv-qa.blogspot.com/2017/07/restassured-short-example.html

Problemi correlati