Json与RestAssured的完整比赛 [英] Full Json match with RestAssured

查看:140
本文介绍了Json与RestAssured的完整比赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST-Assured测试一些API.我的API显然会使用JSON进行响应,并根据文档进行响应(如果这是响应):

I'm using REST-Assured to test some API. My API clearly respond with a JSON and according to the doc if this is the response:

{
    "id": "390",
    "data": {
        "leagueId": 35,
        "homeTeam": "Norway",
        "visitingTeam": "England",
    },
    "odds": [{
        "price": "1.30",
        "name": "1"
    },
    {
        "price": "5.25",
        "name": "X"
    }]
}

我可以这样测试:

@Test
public void givenUrl_whenSuccessOnGetsResponseAndJsonHasRequiredKV_thenCorrect() {
   get("/events?id=390")
      .then()
         .statusCode(200)
         .assertThat()
            .body("data.leagueId", equalTo(35)); 
}

当然这是可读的,但我将对JSON进行全面比较(即:这是JSON响应;这是固定的JSON-资源文件将是完美的-这些JSON等于吗?). REST-Assured是否提供类似的功能,或者我需要手动制作.

Surely this is readable but I would a full comparison of the JSON (i.e.: this is the JSON response; this is a canned JSON - a resource file would be perfect - are those JSON equals?). Does REST-Assured offer something like that or I need to make it manually.

推荐答案

我遇到了同样的问题,并使用RestAssured的JsonPath解决了该问题,将json文件解析为Map,然后将其与Hamcrest Matchers进行了比较.这样,顺序等就没关系了.

I had the same problem, and resolved it with RestAssured's JsonPath to parse the json file into a Map and then compare it with Hamcrest Matchers. This way the order etc didn't matter.

import static org.hamcrest.Matchers.equalTo;
import io.restassured.path.json.JsonPath;

...

JsonPath expectedJson = new JsonPath(new File("/path/to/expected.json"));

given()
    ...
    .then()
    .body("", equalTo(expectedJson.getMap("")));

这篇关于Json与RestAssured的完整比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆