[英]How can I test a spring-cloud-contract containing a java.time.Instant field
我想测试一个字段为java.time.Instant类型的合同。 但是,并非所有的Instant实例都可以像我期望的那样由spring-cloud-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())
}
}
}
以及此服务的实现:
@RestController
public class VersionController {
@GetMapping(path = "/version")
public ResponseEntity<Version> getCurrentVersion() {
return ResponseEntity.ok(new Version(42, Instant.ofEpochMilli(0)));
}
}
执行gradle测试效果很好。 但是,如果我用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)/)]]
这是可以理解的,因为Instant.now()生成一个Instant,其字符串表示形式确实与anyIso8601WithOffset()
模式不匹配。 但是为什么呢? 为什么即时消息的表示方式有所不同,我如何描述可以对即时消息进行验证的合同?
好的,我找到了适合我的解决方案。 虽然我不知道这是不是要走的路。
为了始终获得完全相同的序列化即时格式,我定义了我的版本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.