Java 是否尝试返回预期和实际匹配的API响应的id?

Java 是否尝试返回预期和实际匹配的API响应的id?,java,testng,rest-assured,Java,Testng,Rest Assured,我一直在测试一个api,我在测试中寻找了ID 24,我的实际和预期都是一样的,我假设我使用了不正确的语法 @Test public void test_get_userid() { given(). when(). get("http://bpdts-test-app-v2.herokuapp.com/user/24"). then(). assertThat()

我一直在测试一个api,我在测试中寻找了ID 24,我的实际和预期都是一样的,我假设我使用了不正确的语法

    @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);