Contract Testing Boilerplate has two microservices developed using Spring Boot in a maven multi-module project.
An integration contract test is a test at the boundary of an external service verifying that it meets the contract expected by a consuming service.
- Date Provider MicroService - /provider/validDate - Validates whether given date is a valid date or not
- Age Consumer MicroService - /age-calculate - Returns age of a person based on given date
Start Date Provider MicroService by default runs in port 8080:
mvn spring-boot:run -pl date-provider
Start Age Consumer MicroService by default runs in port 8081:
mvn spring-boot:run -pl age-consumer
Click to expand!
Pact is a contract testing tool. Contract testing is a way to ensure that services (such as an API provider and a client) can communicate with each other. Without contract testing, the only way to know that services can communicate is by using expensive and brittle integration tests.
mvn clean test -pl age-consumer
Generated Pact file:
{
"provider": {
"name": "dateProvider"
},
"consumer": {
"name": "ageConsumer"
},
"interactions": [
{
"description": "valid date from provider",
"request": {
"method": "GET",
"path": "/provider/validDate",
"query": {
"date": [
"2001-02-03"
]
},
"matchingRules": {
"query": {
"date": {
"matchers": [
{
"match": "date",
"date": "2001-02-03"
}
],
"combine": "AND"
}
}
},
"generators": {
"body": {
"date": {
"type": "Date",
"format": "2001-02-03"
}
}
}
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json",
"Content-Type": "application/json; charset=UTF-8"
},
"body": {
"month": 8,
"year": 2000,
"isValidDate": true,
"day": 3
},
"matchingRules": {
"body": {
"$.year": {
"matchers": [
{
"match": "number"
}
],
"combine": "AND"
},
"$.month": {
"matchers": [
{
"match": "number"
}
],
"combine": "AND"
},
"$.day": {
"matchers": [
{
"match": "number"
}
],
"combine": "AND"
},
"$.isValidDate": {
"matchers": [
{
"match": "type"
}
],
"combine": "AND"
}
},
"header": {
"Content-Type": {
"matchers": [
{
"match": "regex",
"regex": "application/json(;\\s?charset=[\\w\\-]+)?"
}
],
"combine": "AND"
}
}
}
},
"providerStates": [
{
"name": ""
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "4.0.9"
}
}
}
docker-compose up -d
$ cd age-consumer
$ mvn pact:publish
mvn clean -Dtest=PactAgeProviderTest test -pl date-provider
Click to expand!
Spring Cloud Contract is an umbrella project holds solutions to help users implement contract tests. It has two main modules:
- Spring Cloud Contract Verifier, which is used mainly by the producer side.
- Spring Cloud Contract Stub Runner, which is used by the consumer side.
- Consumer Driven Contract Testing [Webinar Recording]
- Introduction to consumer-driven contracts
- Consumer-Driven Contract Testing using Pact Java
- Consumer-Driven Contract Testing using Spring Cloud Contracts
- Event-Driven Architecture: How to Perform Contract Testing in Kafka/pubSub
- Integrating Contract Testing in Build Pipelines