【问题标题】:rest-assured expected doesn't match放心预期不匹配
【发布时间】:2015-07-23 05:30:56
【问题描述】:

我试试

@Test
public void testGetSingleUser() {
    given().expect().
        statusCode(200).contentType(ContentType.JSON).
        body("_testingString", equalTo("got it")).when().
        get("http://localhost:8080/getIt");
}

但总是收到此错误消息

java.lang.AssertionError : JSON 路径 _testingString 不匹配。

预期:“得到它”得到:[得到它]

如何忽略""和[]的问题 谢谢

【问题讨论】:

    标签: web-services rest rest-assured


    【解决方案1】:

    注意:这是未经测试的代码 sn-p 但在查看错误跟踪时可能会对您有所帮助。

    试试

    equalTo(Arrays.asList("got it"))
    

    而不是

    equalTo("got it")
    

    【讨论】:

    • 如果我尝试使用 int 类型,它似乎效果不佳...预期 ,但得到 [1]
    • 你把 int 值放在数组里面了吗?
    • 私有列表 list = new ArrayList();模型 w1 = new Model("知道了",1);
    • 你的这段代码没有给我任何线索。如果可能的话,然后提供更多代码,那么我会帮助你,因为你的这两行似乎对我没有害处
    • 所以我尝试在这里复制我的大部分代码stackoverflow.com/questions/31643260/… 谢谢。
    【解决方案2】:

    当你收到一个错误提示时,类似这样:

    JSON 路径总数不匹配。 预计:2000 实际:

    这意味着你的结果(实际:) 进入了一个数组。

    因此,您应该使用一个可靠的命令(例如,hasItem)来验证“数组内”的值。

    我下面的代码说明了这种情况的解决方案:

        RestAssuredWebTestClient
    
             .given()
             .webTestClient(mockedWebClient)
             .queryParam("cost", 1000)
    
             .when()
             .get(TEMPL_AGGREG_DATE)
    
             .then()
             .log()
             .everything()
    
             .statusCode(OK.value())
             .body("_id", hasItem(project2.getStartDate()))
             .body("total", hasItem((int)project2.getEstimatedCost()))
             .body(matchesJsonSchemaInClasspath("contracts/aggregations/CostsGroupByStartDate.json"))
        ;
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多