0

//Below mentioned code not working for assertion in google api

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
public class Base {

public static void main(String[] args) {
    //URL --> End Point --> BaseURL/Resources/Format?Parameters
    // BaseURL or Host
    RestAssured.baseURI = "https://maps.googleapis.com";
    given().
            param("location","33.8670522,151.1957362").
            param("radius","500").
            param("key","AIzaSyDahQkqdxmUihrC0_3Gi7hRBZQWDrV1xI0").
            when().
            get("/maps/api/place/nearbysearch/json").
            then().assertThat().statusCode(200).and().contentType(ContentType.JSON).and().
            body("results[0].geometry.location.lat", equalTo("-33.8710748"));            
}
}

Below error I am getting : java.lang.AssertionError: 1 expectation failed. JSON path results[0].geometry.location.lat doesn't match. Expected: -33.8710748 Actual: null

Also find the original response:

https://jsoneditoronline.org/?id=7f9b24fa65f044fa9c4f48500a6c9bbe

Subburaj
  • 2,294
  • 3
  • 20
  • 37
Anubhav Jain
  • 55
  • 11
  • It looks mentioned JSON Path is correct only. Whether you are getting the valid response? – Subburaj Jun 02 '18 at 10:37
  • I don't understand your reply, please elaborate, as I am new to this API testing. In eclipse Actual result is coming as null. – Anubhav Jain Jun 02 '18 at 11:37
  • I hope all the requested query parameter is correct.So, Please extract the response first and then print(added the sample in answer). There could be a Valid Failure – Subburaj Jun 02 '18 at 14:53

1 Answers1

1

It looks, Given Piece of Code is correct only (I hope all the applicable headers are mentioned)and valid response might not be retrieved from the API with Status Code as 200. Please extract the response and store in one variable for the debugging purpose.

Please check the below for the debugging.

   public static void main(String[] args) {
    //URL --> End Point --> BaseURL/Resources/Format?Parameters
    // BaseURL or Host
    RestAssured.baseURI = "https://maps.googleapis.com";
    String result=given().
            param("location","33.8670522,151.1957362").
            param("radius","500").
            param("key","AIzaSyDahQkqdxmUihrC0_3Gi7hRBZQWDrV1xI0").
            when().
            get("/maps/api/place/nearbysearch/json").
            then().assertThat().statusCode(200).extract().asString();;

    System.out.println(" API Response :"+ result);
}
Subburaj
  • 2,294
  • 3
  • 20
  • 37