使用 Hamcrest-Date 和 DateTimeFormatter 来放心地进行断言。

huangapple go评论69阅读模式
英文:

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(&quot;startDate&quot;, LocalDate.now().minusMonths(1).format(DateTimeFormatter.BASIC_ISO_DATE)).
			param(&quot;endDate&quot;, LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)).
			pathParam(&quot;accountid&quot;, accountId_DP).

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

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

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

</details>



huangapple
  • 本文由 发表于 2020年4月11日 03:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61147315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定