【问题标题】:Hamcrest exception message while actual and expected are sameHamcrest 异常消息,实际和预期相同
【发布时间】:2018-03-25 17:00:19
【问题描述】:

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

@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....

【问题讨论】:

  • 这可能意味着尾随空格或某些字符,使您在视觉上看起来相同。请检查没有额外的空格或其他东西
  • 实际上输出只证明了预期和实际产生了相同的 toString() 表示,它们不一定是相同的类型。顺便问一下,响应正文属于哪种内容类型?
  • 响应为 JSON 格式。

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


【解决方案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."));}

【讨论】:

    【解决方案2】:

    啊,因为每个人都可以访问您使用的 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.")));
    }
    

    注意各种可能的断言:

    • 列表的大小断言
    • 单项断言
    • 整个列表的完整断言

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 2023-03-19
      • 2020-10-09
      相关资源
      最近更新 更多