英文:
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
英文:
I have been playing around testing an api I've looked for ID 24 in my test and my actual and expected are the same I'm assuming I'm using the incorrect syntax where I have .body what should I put in place instead?
@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
答案1
得分: 1
提供的JSON对象在键"id"
下有数字24
。它不是一个JSON字符串,那应该是"24"
。
您尝试过equalTo(24)
吗?就像是一个数字文字,而不是一个字符串文字。
英文:
The provided JSON object has the number 24
under the key "id"
. It is not a JSON string, that would be "24"
.
Have you tried equalTo(24)
? As in a number literal, not a string literal.
答案2
得分: 0
类型为整数,而不是字符串。
正确答案是:
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);
英文:
Type of id is an integer, not String.
Correct Answer is
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));
You can test using the following code which will give the type of the value as "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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论