Skip to content
Advertisement

Spring Cloud Contract: how to prevent Producer to break consumers?

I am new to Spring Cloud Contracts and recently I was able to generate Producer contracts, and write a test on the consumer side using the stubs installed on my local maven repo.

If I break the contract on the Producer side and then install the new stubs, as expected if I run the consumer tests it will fail.

But imagine this scenario:

  • Producer is changed, breaking contracts in consumer-a
  • Tests on Producer pass so build succeeds and eventually is deployed
  • Later consumer-a build fail because of broken contract on producer

Does it make sense, during producer build, somehow executed all tests of the consumers of that producer? If so how?

How can we avoid that a producer breaks a consumer?

Thank you so much for your help.

Regards

Advertisement

Answer

It doesn’t make sense for the producer to execute consumer’s tests because sometimes you don’t even know who is consuming your application, for example, when your API is public.

It’s expected that stubs used by the consumer break tests on the consumer’s side because that’s one of the ways that consumers will know that the contract has changed. So, instead of getting the error in production due to a contract change, you can have fast feedback when the contract tests fail during the consumer’s build.

But, you can have more control on the consumer’s side, passing the version of the stub for your tests instead of getting the latest version:

@AutoConfigureStubRunner(ids = {"<url>:<service>:<VERSION>:stubs:<port>"}, stubsMode = StubRunnerProperties.StubsMode.LOCAL)

The disadvantage is that the consumer’s contract tests may not reflect the producer’s version in production.

Besides, you can also have different contracts for different types of consumers:

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement