Web Services 放心预期不匹配

kjthegm6  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(130)

我尽力了

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

但总是收到此错误消息
Assert错误:JSON路径_testingString不匹配。
预期:“得到了”得到了:[收到]
如何忽略“”和[]问题谢谢

0dxa2lsx

0dxa2lsx1#

**注意:**这是未经测试的代码片段,但在查看错误跟踪时,它可能会对您有所帮助。

尝试

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

代替

equalTo("got it")
332nm8kg

332nm8kg2#

当您收到一个错误消息,内容如下:
JSON路径总数不匹配。应为:2000年实际数:〈[2000年]〉
这意味着您的结果(Actual:)将进入数组。
因此,您应该使用rest-assured-command(例如,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"))
    ;

相关问题