Search code examples
javaspringspring-bootspring-cloudspring-cloud-contract

Spring Cloud Contract : Write Contract Test for multiple consumer with different or partially different expectations


We are doing consumer driven testing using spring-cloud-contract. I have two consumers (services) and one producer service. All of them are in different repositories. The producer has an api e.g. - /get/product/123. Each consumer has different requirements. So, I created two folders and put a getProduct.groovy file in each folder. It looks something like below:

contracts │   ├── consumerA │   │   ├── GetProduct.groovy │   ├── consumerB │   │   ├── GetProduct.groovy

Both groovy files contain different body and generate stubs in the respective folders.


Solution

  • In order for the stubsPerConsumer feature to work, you need to make sure you set its flag to true in the StubRunner setup in your consumer integration tests, like so:

    @AutoConfigureStubRunner(ids = "yourids", repositoryRoot = "yourrepository",
    stubsMode = StubRunnerProperties.StubsMode.REMOTE,stubsPerConsumer = true)