Introduction

The article demonstrates how to write a contract between the producer & the consumer and how to implements the producer & the consumer side test cases for Spring Cloud Contract through an HTTP request between two microservices.

Producer/Provider

The producer is a service that exposes an API (e.g. rest endpoint) or sends a message (e.g. Kafka Producer which publishes the message to Kafka Topic)

Consumer

The consumer is a service that consumes the API that is exposed by the producer or listens to a message from the producer (e.g. Kafka Consumer which consumes the message from Kafka Topic)

Contract

The contract is an agreement between the producer and consumer how the API/message will look like.

  • What endpoints can we use?
  • What input do the endpoints take?
  • What does the output look like?

Consumer-Driven Contract

Consumer-driven contract (CDD) is an approach where the consumer drives the changes in the API of the producer.

Consumer-driven contract testing is an approach to formalize above mentioned expectations into a contract between each consumer-provider pair. Once the contract is established between Provider and Consumer, this ensures that the contract will not break suddenly.

Spring Cloud Contract

Spring Cloud Contract is a project of spring-cloud that helps end-users in successfully implementing the Consumer Driven Contracts (CDC) approach. The Spring Cloud Contract Verifier is used as a tool that enables the development of Consumer Driven Contracts. Spring Cloud Contract Verifier is used with Contract Definition Language (DSL) written in Groovy or YAML.

Demo Application

To understand the concept of the Spring Cloud Contract, I have implemented two simple microservices. The code for these applications can be found on Github account.

request and response

Request and response between the consumer and the producer

Create-employee-application MS

It is the first microservice responsible for creating an employee’s profile based on the given details. We are only passing the FirstName, LastName, and Identification Number (e.g. National ID) of the employee. This microservice is calling another microservice to first check, based on the Identity Number, whether the profile has already been created for the employee.

Get-employee-application MS

This is the second microservice service that is just checking if an employee profile already exists. If the employee profile is matching with the Identification Number provided in the database, it will return the profile else return an empty profile with the EMPLOYEE_NOT_FOUND status.

The create-employee-application microservice is having a dependency on get-employee-application microservice, so we have written a contract of get-employee-application. We are not using any database here to store or retrieve employee details so that written simple logic which will help us to fetch the existing employee profile.

Setup

We are going to understand how we have done the setup for these applications. We are going to discuss the setup in each microservice one by one.

#tutorial #microservices #spring boot #spring cloud #spring boot microservices #spring cloud contract #microservices testing

Consumer-Driven Contract Testing With Spring Cloud Contract
3.45 GEEK