请放心接收实际值的null,但Postman显示值存在

问题描述 投票:0回答:2

我正在使用放心(java)自动化谷歌地方api,我的结果有问题,我似乎并不理解。

所以基本上当我使用postman执行以下URL的GET请求时:

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

它输出结果如下:

    "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
}

但是,当我使用放心来检查此请求的响应时,它表示我的断言中存在错误。它指出:

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

我不确定为什么'Actual'显示为null,因为邮递员显示有响应,似乎我的代码是正确的,并且正在检查响应的正确位置:

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");
    }


}

任何人都可以看到为什么实际结果显示为null?

java postman rest-assured
2个回答
0
投票

您可以使用REST保证的日志记录功能,以防无法检查自己在响应正文中的确实内容:log if validation fails


-1
投票

无论何时在纬度或经度上进行相等比较,它都不会传递,因为基础值每次都在不断变化。

使用containString解决了这个问题,应该适合你:

.body("results[0].geometry.viewport.northeast.lat", containString("-33.85723597010728"));
© www.soinside.com 2019 - 2024. All rights reserved.