Microservices are eating the world! The arrival of this concept changed not only the way we're designing our software architecture, but also how teams are formed, how they're organized and how they work together.

One of the many other challenges that Microservices brings, is the way we test changes made on them. Martin Fowler and James Lewis Introduced on their definition of Microservices the concept of Consumer-Driven Contract Testing:

Executing consumer driven contracts as part of your build increases confidence and provides fast feedback on whether your services are functioning.

In this quick post, we'll briefly define the concept of CDC, as well as testing a Producer and consumer communicating throught HTTP, using Spring Cloud Contract.

CDC Testing

Consumer Driven Contract approach is nothing more than an agreement, to test integration points, between the Server (Consumer) and Client (Provider) about the format of data that they communicate between each other, eliminating the hassle of end to end tests.

Spring Cloud Contract is an amazing framework that facilitates consumer driven contract tests.

Show me the code

Server / Producer side

First we need to add spring-cloud-starter-contract-verifie to our Producer pom and configure the spring-cloud-contract-maven-plugin with the base class for tests, which I will describe a bit later.

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RC1</spring-cloud.version> </properties> <dependencies> ... <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-contract-verifier</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-contract-maven-plugin</artifactId> <extensions>true</extensions> <configuration> <baseClassForTests>me.aboullaite.spring.cloud.springcloudcontractproducer.BookApiBase</baseClassForTests> </configuration> </plugin> </plugins> </build>

Our simple producer is in the form of BookController , that exposes HTTP REST APIs for managing books.