Search code examples
springspring-cloud-contract

spring cloud contract - tour - request body issue


I'm trying to understand spring cloud contract so am reading the tour.
My question is specifically regarding this section:
https://cloud.spring.io/spring-cloud-contract/multi/multi__spring_cloud_contract_verifier_introduction.html#_defining_the_contract

Looking at the request body part, there is a difference between the Groovy and YAML contracts.
In the Groovy DSL:

"client.id": $(regex('[0-9]{10}'))

In the YAML:

"client.id": 1234567890

You can see the difference - one is specific, the other is not.

Questions are:
Q1. Is this difference deliberate?
Q2. Is it correct that these are turned into tests on the Producer side, so they are run against my real implementation of the service?
So does that mean my real implementation needs to support the hard coded client.id of 1234567890?

TIA


Solution

  • The difference is such that in Groovy DSL we can code sth and YAML is just declarative. In Groovy, in the body, we can state that the value of a particular part of the body will be dynamic. Then at runtime we will generate the fixed value (e.g. for the generated tests). You could also achieve the same via the bodyMatchers section.

    In Yaml however you can't code. You need to provide a fixed value and if you want to have a dynamic part of that particular e.g. body element then you have to provide that value in the matchers sections. That will tell the framework that there is a fixed value for e.g. generated tests BUT also there's a dynamic part to consider e.g. for the stub.