英文:
Rest Assured with Hamcrest-Date and DateTimeFormatter
问题
以下是翻译好的内容:
我有一个使用Rest Assured编写的测试,在这个测试中,我试图验证响应中的日期是否在传入的日期参数之间。我尝试使用Hamcrest-Date来进行验证,但是我json响应中返回的日期是yyyy-MM-dd格式的,而Hamcrest-Date似乎是以Day, Date Month Year的格式。我不能在body断言中使用.format(DateTimeFormatter.BASIC_ISO_DATE),因为DateTimeFormatter会将其转换为字符串,而Hamcrest-Date似乎不支持这种格式。这应该如何解决?
这是代码中的错误部分:
"The method sameOrBefore(Date) in the type DateMatchers is not applicable for the arguments (String)"
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();
}
英文:
I have a Rest Assured test where I am trying to validate that the dates in the response are between the date params passed in. I am trying to use Hamcrest-Date but the dates passed back in my json response are in yyyy-MM-dd and Hamcrest-Date seems to be in Day, Date Month Year. I cannot use .format(DateTimeFormatter.BASIC_ISO_DATE) in the body assertion because DateTimeFormatter converts it into a String and Hamcrest-Date doesnt seem to support it. How could this work?
This is the error in the code:
"The method sameOrBefore(Date) in the type DateMatchers is not applicable for the arguments (String)"
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
得分: 1
以下是已经翻译好的内容:
自己回答自己的问题,以防将来有人遇到类似情况。我找到的答案是不要使用 Hamcrest-Date,而是要使用 Hamcrest Matcher 中的 lessThanOrEqualTo/greaterThanOrEqualTo。
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 测试前缀,以在控制台中查找 {}", 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();
<details>
<summary>英文:</summary>
Answering my own question in case anyone ever runs into this. The answer I found was to not use Hamcrest-Date but was to use Hamcrest Matcher lessThanOrEqualTo/greaterThanOrEqualTo
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();
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论