1

I want to test a contract where one field is of type java.time.Instant. But not all instances of an Instant are handled as I expect by 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. But if I replace the Instant with Instant.now(), my provider test fails with

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. But why is this? Why are Instants represented differently and how can I describe a contract that validates for any instant?

4

1 回答 1

1

好的,我找到了适合我的解决方案。虽然我不知道这是否是要走的路。

为了始终获得完全相同的序列化瞬间格式,我将版本 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;
  }
} 
于 2018-11-11T19:46:59.647 回答