实际和预期的Hamcrest异常消息是相同的

时间:2018-03-25 17:00:20

标签: selenium rest-assured hamcrest rest-assured-jsonpath

我收到以下异常消息,而实际和预期相同。失败的原因似乎是不正确的。

@Test    
            public static void Verify()
            {
                given().
                get("http://services.groupkt.com/country/get/all").
                then().body("RestResponse.messages", equalTo("[Total [249] records 
            found.]"));}

FAILED: Verify
java.lang.AssertionError: 1 expectation failed.
JSON path RestResponse.messages doesn't match.
Expected: [Total [249] records found.]
  Actual: [Total [249] records found.]

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) and much more....

2 个答案:

答案 0 :(得分:1)

@Prasad:由于字符串字符我怀疑它。试试这个应该有效的代码

    @Test
public void Verify()
{
    given()
            .get("http://services.groupkt.com/country/get/all")
            .then()
            .body("RestResponse.messages[0]",equalTo("Total [249] records found."));}

答案 1 :(得分:0)

啊,因为每个人都可以访问您使用过的URL,我可以为您提供此解决方案:

@Test
public void restAssured() {
    RestAssured.given()
            .accept(ContentType.JSON)
            .get("http://services.groupkt.com/country/get/all")
            .then()
            .statusCode(200)
            .body("RestResponse.messages", hasSize(1))
            .body("RestResponse.messages[0]", is("Total [249] records found."))
            .body("RestResponse.messages", is(Arrays.asList("Total [249] records found.")));
}

注意可能的各种断言:

  • 列表的大小断言
  • 单项断言
  • 完整断言整个清单
相关问题