0
votes

I need to do simple test using REST-assured. This is the link to google maps API http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false

and i need to verify that statusCode for that request is correct (200) and fields: status: "OK" type is "street_address" country is "US"

I wrote this code but it is partly working

public class RestTest {
@Test
public void Test1() {


    RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
            statusCode(200).
            contentType(ContentType.JSON).
            body("status", equalTo("OK")).
            body("results.types", equalTo("street_address")).
            body("results.address_components.short_name", equalTo("US"));

Check status code, content type and first part of body (status=OK) works well and pass the test but i had problems with last two body tests, they're failing and i get:

JSON path results.types doesn't match.
Expected: street_address
Actual: [[street_address]]
1

1 Answers

0
votes

In the response results is an array and address_components is also an array. So in your json path you have to specify the index of the element. The below solution would work.

@Test
public void Test1() {
RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
          statusCode(200).
          contentType(ContentType.JSON).
          body("status", equalTo("OK")).
    body("results[0].types", contains("street_address")).
    body("results[0].address_components[5].short_name", equalTo("US"));

}