How can I test a spring-cloud-contract containing a java.time.Instant field

825 views Asked by At

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?

1

There are 1 answers

0
Tobias Neubert On BEST ANSWER

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:

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;
  }
}