简体   繁体   English

如何测试包含java.time.Instant字段的spring-cloud-contract

[英]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.

相关问题 Spring MockMvc - 从REST获取java.time.Instant - Spring MockMvc - getting java.time.Instant from REST 如何将 @kotlinx.serialization.Serializable 与 java.time.Instant 一起使用? - How to use @kotlinx.serialization.Serializable with java.time.Instant? 无法反序列化`java.time.Instant`类型的值 - jackson - Cannot deserialize value of type `java.time.Instant` - jackson 如何使用Spring Cloud Contract从Groovy中的RESTful服务调用获取/打印JSON响应 - How can I get/print the JSON response from a RESTful service call in Groovy with Spring Cloud Contract com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造`java.time.Instant`的实例(无创建者,如默认构造): - com.fasterxml.jackson.databind.exc.InvalidDefinitionException:Cannot construct instance of`java.time.Instant`(no Creators,like default construct): 如何在编译时以编程方式创建验证合同? - How can I programmatically create a validation contract at compile-time? Spring 启动:测试响应实体包含 Java 时间 - Spring Boot: Testing ResponseEntity Containing Java Time 在 Spring Cloud Contract Groovy 合约中使用复杂的 JSON - Using Complex JSON in Spring Cloud Contract Groovy contract 我如何解析 json object 在 java 中包含斜杠 - How can i parse json object containing slashes in java 如何在jQuery手风琴中实现即时搜索 - How can I implement instant search within jQuery Accordions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM