Improve the design and testing of your micro-services through Consumer-Driven Contract Tests (1/3)

Why should you read this article ?

If you develop or maintain micro-services you already asked yourself those questions :

How can we test this simple integration ?


We can use a mock of the API to design our client integrations. With mocks, we do not need to set up a whole runtime environment. We can run isolated tests between the client and a mock of the API.

Limits of mocking

  • What happens if there is a change in the real producer ?

End to end testing

To test interfaces between a client and an API the most used technique is end-to-end testing. In end-to-end tests (E2E tests), real servers or containers are set up so that client and provider are available in a production-like runtime environment.

Limits of E2E testing

  • Slow

Consumer-Driven Contract approach as an alternative

If we take a look at the definition of an integration test : “An integration test is a test between an API provider and an API consumer that asserts that the provider returns expected responses for a set of pre-defined requests by the consumer. The set of pre-defined requests and expected responses is called a contract.”

The whole idea behind CDC is to involve consumers of the future API in the creation of the API itself. API changes will be driven by consumers.

Glossary :

  • Producer : Service that exposes an API.
  • Consumer : Service that consumes the API of the producer.
  • Contract : Agreement between producer and consumer on how the API will look like.
  • Consumer Driven Contracts : Approach where the consumer drives the changes of the API of the producer.
Thoughtworks point of view on CDC testing.

How does it work ?

  • Define API contract on the consumer side.

Write Unit tests that define clearly what are the interactions and behaviors expected from the provider.

  • Enforce the contract on the service provider side.

Unit tests on the provider side ensure that the provider implementation respect the expectations (contract) defined by the consumer.

Benefits of CDC

CDC makes your life easier :

  • Drive the providers implementation through the contract

Start by enforcing the contract without the API, then design the API accordingly : you will write less code and avoid extra features.

  • Help providers make changes without being scared of accidentally breaking their consumers
  • Let consumer know that the APIs they consume won’t suddenly break
  • Allows consumers to develop against API definitions before the provider API has actually been developed : Design first approach

Get the Medium app