请放心,收到实际值的空值,但邮递员显示存在值



我正在使用放心(java(自动化谷歌放置api,我的结果遇到了问题,我似乎不明白。

所以基本上当我使用邮递员对以下 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

我不确定为什么"实际"显示 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");
}

}

有人能看到为什么实际结果显示空吗?

您可以使用 REST 放心的日志记录功能,以防无法检查自己是否确实在响应正文中: 验证失败时的日志

每当您对纬度或经度进行相等比较时,它永远不会通过,因为基础值每次都在不断变化。

使用containString可以解决这个问题,应该适合您:

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

最新更新