【问题标题】:API Automation : Assertion is not working for google place apiAPI 自动化:断言不适用于 google place api
【发布时间】:2018-06-02 06:08:53
【问题描述】:

//下面提到的代码不适用于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"));            
}
}

我得到以下错误: java.lang.AssertionError: 1 期望失败。 JSON 路径结果[0].geometry.location.lat 不匹配。 预期:-33.8710748 实际:空

还可以找到原始回复:

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

【问题讨论】:

  • 看起来提到的 JSON 路径只是正确的。您是否收到了有效的回复?
  • 我不明白你的回复,请详细说明,因为我是这个 API 测试的新手。在 Eclipse 中,实际结果为 null。
  • 我希望所有请求的查询参数都是正确的。所以,请先提取响应然后打印(在答案中添加示例)。可能存在有效失败

标签: rest soap automation rest-assured rest-assured-jsonpath


【解决方案1】:

看起来,Given Piece of Code 仅是正确的(我希望提及所有适用的标头)并且可能无法从 API 中检索到状态码为 200 的有效响应。请提取响应并将其存储在一个变量中调试目的。

请检查下面的调试。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 2015-07-28
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2014-05-26
    相关资源
    最近更新 更多