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

Viewed 696

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.

2 Answers

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)

Adding more to @OlgaMaciaszek,

There is possiblity of specifying consumerName which must be the same as the sub-folder name in producers /contract folder.

One may choose to skip providing the consumerName by using application name specified in application.yaml. But this also means that the subfolder in producer's contract folder must be same as the application name of consumer.

Related