1 of 18

Consumer Driven Contract testing

Jaroslav Holaň, Zonky

jOpenSpace 2017

2 of 18

Design by contract

3 of 18

Pact.io

Spring Cloud Contract

4 of 18

Pact.io

Spring Cloud Contract

5 of 18

6 of 18

7 of 18

8 of 18

9 of 18

User Service

Admin Service

request

response

10 of 18

User Service

Admin Service

request

response

Contract

request

response

Provider

Consumer

pact file

11 of 18

{

"provider": {

"name": "User Service"

},

"consumer": {

"name": "Admin Service"

},

"interactions": [

{

"description": "The request for the count of users.",

"request": {

"method": "GET",

"path": "/users/count"

},

"response": {

"status": 200,

"body": {

"count": 100

}

} } ] }

12 of 18

pact file

file system

URL

Pact broker

13 of 18

Pact broker service

14 of 18

Mock server

docker start pact-jvm-server

POST http://localhost:9999/create

{

"provider": {

"name": "User Service"

},

"consumer": {

"name": "Admin Service"

},...}

{

"port": 20007

}

�GET http://localhost:20007/users/count

15 of 18

Test consumer

@Test�public void whenCallProvider_returnValidMessage() {� ConsumerService consumer = createConsumer();� String response = consumer.callProvider();� assertEquals("Hello from service two", response);�}

org.junit.ComparisonFailure:�Expected :Hello from service two�Actual :Hello from service 2

  1. Run Mock server
  2. Run tests

16 of 18

Test provider

  1. Run provider
  2. mvn pact:verify

17 of 18

docs.pact.io

18 of 18

�JaroslavHolaň

Software Magician at