【问题标题】:Rest-assured simple test with Google Maps API使用 Google Maps API 进行放心的简单测试
【发布时间】:2016-12-13 10:24:54
【问题描述】:

我需要使用 REST-assured 进行简单测试。这是谷歌地图 API 的链接 http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false

我需要验证该请求的 statusCode 是否正确 (200) 和字段: 状态:“好的” 类型是“street_address” 国家是“美国”

我写了这段代码,但它部分工作

public class RestTest {
@Test
public void Test1() {


    RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
            statusCode(200).
            contentType(ContentType.JSON).
            body("status", equalTo("OK")).
            body("results.types", equalTo("street_address")).
            body("results.address_components.short_name", equalTo("US"));

检查状态代码、内容类型和正文的第一部分 (status=OK) 运行良好并通过了测试,但我在最后两次正文测试中遇到了问题,它们都失败了,我得到了:

JSON path results.types doesn't match.
Expected: street_address
Actual: [[street_address]]

【问题讨论】:

    标签: rest api testing automated-tests rest-assured


    【解决方案1】:

    在响应结果中是一个数组,address_components 也是一个数组。 因此,在您的 json 路径中,您必须指定元素的索引。以下解决方案可行。

    @Test
    public void Test1() {
    RestAssured.get("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway+Mountain+View+CA&sensor=false").then().
              statusCode(200).
              contentType(ContentType.JSON).
              body("status", equalTo("OK")).
        body("results[0].types", contains("street_address")).
        body("results[0].address_components[5].short_name", equalTo("US"));
    

    }

    【讨论】:

    • 您需要在 'body("results[0].types", contains("street_address")) 中添加元素的索引。太像'body("results[0].types[0]", contains("street_address")).'之后它工作得很好。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多