In a distributed system, one of the challenges is to continuously test the integration between components. Let’s take a simple challenge to test one api to api integration. For the sake of discussion, we will call them producer api and consumer api.

We could spin up both the apis to test, which we generally call integration testing. Integration testing is great, it is testing the real apis. But the producer api itself may depend on other api(s) or a database or something else. We will need to spin all of this infrastructure to spin it up, which could not only be painful, our tests will be slow.

Another option is to mock the producer api in our consumer tests. Mocking is great and the tests run fast, but there are few challenges with mocks.

Let’s say we mock the producer api in our tests and are happy about it. The producer api, like any other api will evolve, new features get added or bugs get fixed. As a consequence of this, it can start diverging from what we have mocked. When this happens, our mock tests will still pass, but the real integration between the services may break and we may not know until it is too late.

Spring Cloud Contract Verifier

Spring Cloud Contract Verifier is a tool that enables Consumer Driven Contract (CDC). It is shipped with Contract Definition Language (DSL) written in Groovy or YAML.

Let’s take a look at how we can use it. Complete source code is here in github.

Add below dependencies to the Producer api project

implementation platform ("")

name our base test class in build.gradle

contracts {
baseClassForTests = "com.phani.spring.cloudcontracts.spec.BaseContractTest"

create the base test class

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public abstract class BaseContractTest {
private BooksController booksController;

public void setup() {
StandaloneMockMvcBuilder standaloneMockMvcBuilder
= MockMvcBuilders.standaloneSetup(booksController);

create a contract using Spring Cloud Contract DSL

Contract.make {
description "should return book for id 1"
method GET()
response {
id: 1,
name: $(anyNonBlankString()),
author: $(anyNonBlankString())
status 200
headers {
header 'Content-Type': applicationJson()

thats it! This contract will be automatically validated as part of unit tests.

In the client project, add below dependencies

testCompile group: '', name: 'spring-cloud-contract-wiremock', version: '2.1.1.RELEASE'
testCompile group: '', name: 'spring-cloud-contract-stub-runner', version: '2.1.1.RELEASE'

create a test with below annotations

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
stubsMode = StubRunnerProperties.StubsMode.LOCAL,
ids = "com.phani.spring:server:+:stubs:8090")
public class BooksControllerIntegrationTests {

private BooksClient booksClient;

public void getReturnsAbook() throws Exception {
Book book = booksClient.getBook(1);

Spring Cloud Contracts will be automatically stand up a mock producer api for the unit tests.

Let’s say we inadvertently changed the type of our version field on the Book. Our contact test will fail right in the Producer api with below error, which is awesome!

java.lang.IllegalStateException: Parsed JSON [{"id":1,"name":"The Hunger Games","author":"Suzanne Collins","version":"v23"}] doesn't match the JSON path [$[?(@.['version'] =~ /-?(\d*\.\d+|\d+)/)]]
at com.toomuchcoding.jsonassert.JsonAsserter.check(
at com.toomuchcoding.jsonassert.JsonAsserter.checkBufferedJsonPathString(
at com.toomuchcoding.jsonassert.JsonAsserter.matches(
at com.phani.spring.cloudcontracts.spec.ContractVerifierTest.validate_shouldReturnBook(

Complete source code is here in github


As we have seen, Spring Cloud Contract Verifier is a great tool to ensure the integration is not broken between apis.


Spring’s official page on Spring Cloud Contract

An Intro to Spring Cloud Contract

Software professional with a passion for new technologies