Contract Testing Boilerplate for the microservices developed using Spring Boot and Kafka in a maven multi-module project.
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.
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
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"
}
}
}
:8282
docker-compose up -d
$ cd age-consumer
$ mvn pact:publish
mvn clean -Dtest=PactAgeProviderTest test -pl date-provider
Spring Cloud Contract is an umbrella project holds solutions to help users implement contract tests. It has two main modules: