Spring Cloud Contract


Spring Cloud Contract is a project that, simply put, helps us write Consumer-Driven Contracts (CDC).

This ensures the contract between a Producer and a Consumer, in a distributed system – for both HTTP-based and message-based interactions.


Producer – Server Side

For our producer side, we'll need the spring-cloud-starter-contract-verifier dependency.

Producer Side Setup

Test stubs are in /src/test/resources/contracts/ package.

When the build is run, the plugin automatically generates a test class named ContractVerifierTest that extends our BaseTestClass and puts it in /target/generated-test-sources/contracts/. The names of the test methods are derived from the prefix “validate_” concatenated with the names of our Groovy test stubs. For the above Groovy file, the generated method name will be “validate_find_provider_by_id()”.


Consumer – Client Side

The consumer side of our CDC consume stubs generated by the producer side through HTTP interaction to maintain the contract, so any changes on the producer side would break the contract.

The consumer, needs spring-cloud-contract-wiremock and spring-cloud-contract-stub-runner dependencies

Consumer Side Setup

Test needs to configure the stub runner, which will inform the consumer of the available stubs in our local Maven repository

In summary Spring Cloud Contract can help us maintain contracts between a service consumer and producer so that we can push out new code without any worry of breaking the contracts.


Go to source code

Comments

Popular posts from this blog

Debug Java Stream in Intellij Idea

Creating Efficient Docker Images with Spring Boot 2.3

CRUD Goes Even Easier With JPABuddy