【问题标题】:Is there any opportunity to add an assertion message in Rest Assured method?是否有机会在 Rest Assured 方法中添加断言消息?
【发布时间】:2021-11-07 17:59:02
【问题描述】:

是否有机会使用 Rest Assured 库将断言消息添加到我的方法中?例如,我想在这个链方法中写一个断言消息,而不使用 System.out.println()。

given().spec(requestSpecBuilder).when().get(commentsEndPoint).then().
            spec(responseSpecification).assertThat().contentType(ContentType.JSON.withCharset("UTF-8"))

【问题讨论】:

    标签: java rest-assured hamcrest


    【解决方案1】:

    您可以使用 org.hamcrest. describeAs 添加断言消息:

        given().spec(requestSpecBuilder).when().get(commentsEndPoint).then().
                spec(responseSpecification)
                .contentType(describedAs("Error message",
                        equalTo(ContentType.JSON.withCharset("UTF-8"))));
    

    【讨论】:

      【解决方案2】:

      不,Rest-Assured 没有这样的方法。

      要添加断言消息,您可以直接使用 Hamcrest。

      Response res = given().when().get("your_url");
      assertThat("Check status code", res.statusCode(), Matchers.is(200));
      

      【讨论】:

      • 首先,它不是构造函数。二、类MatcherAssert有这个方法public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher)
      猜你喜欢
      • 2015-10-07
      • 2020-10-29
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2020-04-21
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多