CONSUMER

DRIVEN

CONTRACTS

Srinivasan Sekar

Lead Consultant

ThoughtWorks

Monolithic vs Microservices

A sad story ....

Payment

Users

{
    "users":[]

}
{
    "user":[]

}

Test Pyramid

Mocked

Dependencies

Payment Service

Mocked User service

Request

Response

Mocked Test are

Targeted

Cheap

Fast

Not Trustworthy

Integration

Test

Integration test are 

TrustWorthy

Slow

Brittle

Whose fault ?

CONSUMER

DRIVEN

CONTRACTS

  • Formalize the interactions between a consumer and a provider.
  • The consumer creates a contract, which is an agreement between consumer & provider on the interactions that will take place between them
  • The provider has agreed on the contract, both consumer and provider can take a copy of the contract and use tests to verify that implementations don't violate the contract.

How pact works?

A contract between a consumer and provider is called a pact. Each pact is a collection of interactions. Each interaction describes:

 

  • An expected request - describing what the consumer is expected to send to the provider.

 

  • A minimal expected response - describing the parts of the response the consumer wants the provider to return.

Who would typically implement Pact?

  • The consumer team is responsible for implementing the Pact tests in the consumer codebase that will generate the contract.

 

  • The provider team is responsible for setting up the Pact verification task in the provider codebase, and for writing the code that sets up the correct data for each provider state described in the contract.

 

  • Both teams are responsible for collaborating and communicating about the API and its usage!

PACT Clients

CONSUMER

API

PRODUCER MOCK

API

Request

Response

CONTRACT TESTING

 

Contract

PRODUCER VERIFIER

PRODUCER

API

Request

Response

Contract

{
   "user":
}
{
   "users":
}

PIPELINES

Demo

Pact

Broker

References

https://martinfowler.com/articles/consumerDrivenContracts.html

https://blog.testproject.io/2020/04/27/introduction-to-consumer-contract-testing/

https://blog.testproject.io/2020/05/27/consumer-driven-contract-testing-using-pact-java/

Conclusion

By formalizing the interaction between consumer and provider, and verifying that both services stick to the contract they’ve agreed upon, we see that we have no need to perform integration tests between them. Instead, we’re using new tests that are specific, fast, and robust.

Consumer Driven Contracts

By Srinivasan Sekar

Consumer Driven Contracts

  • 461