[英]How can I test a spring-cloud-contract containing a java.time.Instant field
I want to test a contract where one field is of type java.time.Instant. 我想测试一个字段为java.time.Instant类型的合同。 But not all instances of an Instant are handled as I expect by spring-cloud-contract.
但是,并非所有的Instant实例都可以像我期望的那样由spring-cloud-contract处理。 Given the following simple contract:
给出以下简单合同:
Contract.make {
description("Get a version")
request {
method 'GET'
url '/config/version'
headers {
contentType(applicationJson())
}
}
response {
status 200
body(
nr: 42,
creationDate: producer(anyIso8601WithOffset())
)
headers {
contentType(applicationJson())
}
}
}
And this service implementation: 以及此服务的实现:
@RestController
public class VersionController {
@GetMapping(path = "/version")
public ResponseEntity<Version> getCurrentVersion() {
return ResponseEntity.ok(new Version(42, Instant.ofEpochMilli(0)));
}
}
Executing gradle test works fine. 执行gradle测试效果很好。 But if I replace the Instant with Instant.now(), my provider test fails with
但是,如果我用Instant.now()替换Instant,我的提供程序测试将失败
java.lang.IllegalStateException: Parsed JSON [{"nr":42,"creationDate":"2018-11-11T15:28:26.958284Z"}] doesn't match the JSON path [$[?(@.['creationDate'] =~ /([0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.\d{3})?(Z|[+-][01]\d:[0-5]\d)/)]]
which is understandable because Instant.now() produces an Instant whose string representation does indeed not match the anyIso8601WithOffset()
pattern. 这是可以理解的,因为Instant.now()生成一个Instant,其字符串表示形式确实与
anyIso8601WithOffset()
模式不匹配。 But why is this? 但是为什么呢? Why are Instants represented differently and how can I describe a contract that validates for any instant?
为什么即时消息的表示方式有所不同,我如何描述可以对即时消息进行验证的合同?
Ok, I found a solution that works for me. 好的,我找到了适合我的解决方案。 Although I do not know if this is the way to go.
虽然我不知道这是不是要走的路。
In order to always get the exact same format of the serialized instant, I define the format of the corresponding property of my version bean as follows: 为了始终获得完全相同的序列化即时格式,我定义了我的版本bean的相应属性的格式,如下所示:
public class Version {
private final int nr;
private final Instant creationDate;
@JsonCreator
public Version(
@JsonProperty("nr") int nr,
@JsonProperty("creationDate") Instant creationDate)
{
this.nr = nr;
this.creationDate = creationDate;
}
public int getNr() {
return nr;
}
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX", timezone = "UTC")
public Instant getCreationDate() {
return creationDate;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.