当前位置: 首页 > 知识库问答 >
问题:

Hamcrest Date和DateTimeFormatter让您放心

方航
2023-03-14

我有一个放心的测试,我试图验证响应中的日期是否介于传入的日期参数之间。我正在尝试使用Hamcrest Date,但在json响应中传递回来的日期是yyyy MM dd,Hamcrest Date似乎是日、日、月、年。我不能用。正文断言中的格式(DateTimeFormatter.BASIC_ISO_DATE),因为DateTimeFormatter将其转换为字符串,而Hamcrest DATE似乎不支持它。这怎么行?

这是代码中的错误:

“类型DateMatchers中的方法sameOrBefore(Date)不适用于参数(字符串)”

        ValidatableResponse vr = given().

                param("startDate", LocalDate.now().minusMonths(1).format(DateTimeFormatter.BASIC_ISO_DATE)).
                param("endDate", LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)).
                pathParam("accountid", accountId_DP).

                header("trace-id", UUID.randomUUID().toString()).
                header("organization", ORGANIZATION).
                header("session-id", SESSION_ID_734548).
                when().
                get("/transactions/{accountid}/pra").           
                then();

        LOGGER.info("test prefix to find in console {}", vr.extract().response().getBody().asString());

        vr.assertThat().statusCode(200).
        and().contentType(ContentType.JSON).
        and().
        body("transactions.postedDate.toString()", everyItem(DateMatchers.sameOrBefore(LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)))).
        and().
        extract().
        response();

    }

共有1个答案

贺飞星
2023-03-14

回答我自己的问题,以防有人遇到这个。我找到的答案是不使用Hamcrest Date,而是使用Hamcrest Matcher less ThanOrEqualTo/Great erThanOrEqualTo


        ValidatableResponse vr = given().

                param("startDate", LocalDate.now().minusMonths(1).format(DateTimeFormatter.BASIC_ISO_DATE)).
                param("endDate", LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)).
                pathParam("accountid", accountId_DP).

                header("trace-id", UUID.randomUUID().toString()).
                header("organization", ORGANIZATION).
                header("session-id", SESSION_ID_734548).
                when().
                get("/transactions/{accountid}/pra").           
                then();

        LOGGER.info("testPRAStartDateEndDate test prefix to find in console {}", vr.extract().response().getBody().asString());

        vr.assertThat().statusCode(200).
        and().contentType(ContentType.JSON).
        and().
        body("transactions.postedDate", everyItem(greaterThanOrEqualTo(LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))))).
        body("transactions.postedDate", everyItem(lessThanOrEqualTo(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))))).
        and().
        extract().
        response();

 类似资料:
  • Debian 和其它的发行版有些不同。即使您熟悉其它发行版的 Linux, 您也应该知道关于 Debian 的某些东西,以便能保持您的系统处于整洁良好的状态。 本章包含了帮助您转向的材料;这并不是一个关于如何使用 Debian 的入门指南, 而仅仅是对整个系统的一个非常概要的介绍。 8.2.1. Debian 的软件包系统 需要掌握的最重要的概念是 Debian 的软件包系统。 事实上,您的系统的

  • Debian 和其它的发行版有些不同。即使您熟悉其它发行版的 Linux, 您也应该知道关于 Debian 的某些东西,以便能保持您的系统处于整洁良好的状态。 本章包含了帮助您转向的材料;这并不是一个关于如何使用 Debian 的入门指南, 而仅仅是对整个系统的一个非常概要的介绍。 8.2.1. Debian 的软件包系统 需要掌握的最重要的概念是 Debian 的软件包系统。 事实上,您的系统的

  • Debian 和其它的发行版有些不同。即使您熟悉其它发行版的 Linux, 您也应该知道关于 Debian 的某些东西,以便能保持您的系统处于整洁良好的状态。 本章包含了帮助您转向的材料;这并不是一个关于如何使用 Debian 的入门指南, 而仅仅是对整个系统的一个非常概要的介绍。 8.2.1. Debian 的软件包系统 需要掌握的最重要的概念是 Debian 的软件包系统。 事实上,您的系统的

  • 因此,我希望这段代码能够在新的Java8date/time包下工作,因为它所做的只是将给定的ZonedDateTime转换成字符串,然后使用相同的内置DateTimeFormatter实例(ISO_INSTANT)返回: 但显然不是: 我已经看到了这个条目,但它对我没有帮助,因为a需要一个ZonedDateTime对象,而不是本地对象,还因为我已经安装了8U20:无法使用DateTimeForma

  • 我对Java中的DateTimeFormatter有问题。 然而,我得到了这个错误: java.time.format.DateTimeParseException:无法分析文本“29/09/2016”:无法从TemporalAccessor:{}获取LocalDateTime,ISO解析为2016-09-29,类型为java.time.format.Parsed],根本原因为java.time.

  • 问题内容: 我正在尝试从Spark中的java.time.format使用DateTimeFormatter,但它似乎不可序列化。这是相关的代码块: 如何避免出现异常?是否有更好的库来解析时间戳?我读过,Joda也不是可序列化的,并且已并入Java 8的时间库中。 问题答案: 您可以通过两种方式避免序列化: 假设其值可以是恒定的,请将格式化程序放在中(使其成为“静态”)。这意味着可以在每个工作程序