Search code examples
spring-cloud-contract

How can I get dynamic response when using Spring Cloud Contract?


org.springframework.cloud.contract.spec.Contract.make {
    request { 
        method 'GET' 
        url '/weather' 
    }
    response {
        status 200
        body([
               "weather": value(regex("(SUNNY|WINDY|CLOUDY|RAINY)"))
        ])
}

I know Groovy DSL is able to generate a random value, Like the code above. But the Groovy DSL just generate a static stub, and it will always return the same response as I requested.

How can I get a random weather without re-generate the stubs in this case?


Solution

  • You can't, that's because WireMock stub needs a concrete value on the response side. The only thing you could do is to reference the request from the response and then the request can have random input. In general, your tests shouldn't depend on random response.