简体   繁体   中英

Rest assured receiving null for actual value but Postman shows value exists

I am automating a google places api using rest assured (java) and I am having issues with my results which I don't seem to understand.

So basically when I use postman to perform a GET request for the following URL:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDWz5aGXygtrt3hsn99yXv_oocw09PSOH

It outputs the results as so:

    "html_attributions": [],
    "results": [
        {
            "geometry": {
                "location": {
                    "lat": -33.8585858,
                    "lng": 151.2100415
                },
                "viewport": {
                    "northeast": {
                        "lat": -33.85723597010728,
                        "lng": 151.2113913298927
                    },
                    "southwest": {
                        "lat": -33.85993562989272,
                        "lng": 151.2086916701072
                    }
                }
            },
            "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png",
            "id": "8e980ad0c819c33cdb1cea31e72d654ca61a7065",
            "name": "Cruise Bar, Restaurant & Events",
            "opening_hours": {
                "open_now": true,
                "weekday_text": []
            },

... //more json
}

However when I use rest assured to check the response of this request, it syas there is an error in my assertion. It states:

Exception in thread "main" java.lang.AssertionError: 1 expectation failed.
JSON path results[0].geometry.viewport.northeast.lat doesn't match.
Expected: -33.85723597010728
  Actual: null

I am unsure why the 'Actual' is displaying null as postman shows there's a response and it seems like my code is correct and is checking the correct place for the response:

package rest.basic.testing;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.equalTo;

public class GetRequestSample {

    //Full URL

    /*https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362
     &radius=1500&type=restaurant&keyword=cruise&key=AIzaSyDWz5aGXygtrt3hsn99yXv_oocw09PSOHE */

    //BaseURI
    static String baseURI = "https://maps.googleapis.com";

    public static void main(String[] args) {
        searchPlaceInGoogle();
    }

    public static void searchPlaceInGoogle() {
        RestAssured.baseURI = baseURI;

        //In the given() we put the parameters which you can see by matching the below with the full URL displayed above

        given()
        .param("location", "33.8670522,151.1957362")
        .param("radius", "1500")
        .param("type", "restaurant")
        .param("key", "AIzaSyDWz5aGXygtrt3hsn99yXv_oocw09PSOHE");
       //In the when we place in our resources which is after the url and before the ?      
        //In the then is our assertions
        when()
        .get("maps/api/place/nearbysearch/json")        
        .then().assertThat().statusCode(200).and()
        .contentType(ContentType.JSON).and()
        .body("results[0].geometry.viewport.northeast.lat", equalTo("-33.85723597010728"));

        //To prove the code above is running successfully
        System.out.println("Request is executed successful");
    }


}

Can anybody see why the actual result is showing null?

您可以使用REST保证的日志记录功能,以防万一无法检查自己在响应正文中的确是什么: 如果验证失败,则记录日志

Whenever you do equality comparisons on latitude or longitude, it will never pass as the underlying values keeps on changing every time.

Using containString gets around that problem and should work for you:

.body("results[0].geometry.viewport.northeast.lat", containString("-33.85723597010728"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM