尝试获取API响应的ID,预期和实际是否匹配?

huangapple go评论87阅读模式
英文:

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(&quot;http://bpdts-test-app-v2.herokuapp.com/user/24&quot;).then().assertThat().statusCode(200).and()
			.contentType(ContentType.JSON).and().body(&quot;id&quot;, equalTo(24));

You can test using the following code which will give the type of the value as "java.lang.Integer"

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

huangapple
  • 本文由 发表于 2020年8月10日 05:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定