试图取回预期和实际匹配的 API 响应的 ID?

Trying to bring back the id of an API response expected and actual match?

我一直在尝试测试 api 我在测试中查找了 ID 24,我的实际值和预期值相同 我假设我使用的语法不正确。 body 我应该用什么代替?

    @Test
public void test_get_userid() {

    given().
            when().
            get("http://bpdts-test-app-v2.herokuapp.com/user/24").
            then().
            assertThat().
            statusCode(200).
            and().
            contentType(ContentType.JSON).
            and().
            body("id", equalTo("24"));
}

java.lang.AssertionError: 1 expectation failed.
JSON path id doesn't match.
Expected: 24
  Actual: 24

提供的 JSON 对象在键 "id" 下有编号 24。它不是 JSON 字符串,而是 "24".

你试过了吗equalTo(24)?就像在数字文字中一样,而不是字符串文字。

id 的类型是整数,不是字符串。

正确答案是

given().when().get("http://bpdts-test-app-v2.herokuapp.com/user/24").then().assertThat().statusCode(200).and()
            .contentType(ContentType.JSON).and().body("id", equalTo(24));

您可以使用以下代码进行测试,该代码会将值的类型指定为“java.lang.Integer”

Class<? extends Object> m1 = given().when().get("http://bpdts-test-app-v2.herokuapp.com/user/24").getBody()
            .jsonPath().get("id").getClass();
    System.out.println(m1);