Testing Java Microservices with Consumer-driven contracts Andrew - - PowerPoint PPT Presentation

testing java microservices with consumer driven contracts
SMART_READER_LITE
LIVE PREVIEW

Testing Java Microservices with Consumer-driven contracts Andrew - - PowerPoint PPT Presentation

Testing Java Microservices with Consumer-driven contracts Andrew Morgan @mogronalol Microservices testing is hard Consumer driven contract testing About Me Independent Consultant specialising in microservices & continuous delivery


slide-1
SLIDE 1

Testing Java Microservices with Consumer-driven contracts

Andrew Morgan @mogronalol

slide-2
SLIDE 2

Microservices testing is hard

slide-3
SLIDE 3

Consumer driven contract testing

slide-4
SLIDE 4

Andrew Morgan @mogronalol

  • Independent Consultant

specialising in microservices & continuous delivery

  • PluralSight Author
  • InfoQ Editor

About Me

slide-5
SLIDE 5
  • Introducing microservices testing

challenges

  • Consumer-driven contract testing
  • verview
  • Following through the workflow

with Spring Cloud Contract

  • Advanced consumer-driven

contract concepts

Agenda

slide-6
SLIDE 6
  • Introducing microservices testing

challenges

  • Consumer-driven contract testing
  • verview
  • Following through the workflow

with Spring Cloud Contract

  • Advanced consumer-driven

contract concepts

Agenda

slide-7
SLIDE 7

Payments Accounts Loans

What are Microservices?

Rewards

slide-8
SLIDE 8

HTTP HTTP

Payments Accounts Loans

What are Microservices?

Rewards

slide-9
SLIDE 9

Messaging HTTP HTTP

Payments Accounts Loans

What are Microservices?

Rewards

slide-10
SLIDE 10
slide-11
SLIDE 11

The root cause of testing challenges

Payments

REST API

Booking

slide-12
SLIDE 12

End-to-end Testing Microservices

Automated tests Browser or other UI Environment

slide-13
SLIDE 13

Build time Debuggability Infrastructure cost Operations Flakiness Distributed monolith

Disadvantages of End-to-end Testing

slide-14
SLIDE 14

Automated tests Microservice Mock or Stub of Dependency

Mocking or Stubbing Dependencies

slide-15
SLIDE 15

False positives Staleness Difficult to maintain

Disadvantages of Mocking or Stubbing

slide-16
SLIDE 16

The Weak Feedback Loop

Run individual microservice tests Build and deploy to stage End-to-end test False positives Release Slow and flaky

slide-17
SLIDE 17
slide-18
SLIDE 18
  • Introducing microservices testing

challenges

  • Consumer-driven contract testing
  • verview
  • Following through the workflow

with Spring Cloud Contract

  • Advanced consumer-driven

contract concepts

Agenda

slide-19
SLIDE 19

What is Consumer-driven Contract Testing?

Fills the microservices testing gap Continuous testing against contracts Consumers drive the implementation of providers Independently testable and releasable microservices

slide-20
SLIDE 20

???

Test Architecture Overview

Consumer Provider

slide-21
SLIDE 21
slide-22
SLIDE 22

Key Contract Characteristics

An agreed interaction between microservices Continuously tested No specific protocol Not the same stubbing! Definitely not API documentation!

slide-23
SLIDE 23

What is an Interaction?

Scenario: Should return no flights between London and Paris

slide-24
SLIDE 24

What is an Interaction?

Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris

slide-25
SLIDE 25

What is an Interaction?

Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris Given Response: 200 []

slide-26
SLIDE 26

The Consumer Side

Automated tests Consuming Microservice Stub of provider microservice Generates The interaction our test depends on

slide-27
SLIDE 27

The Provider Side

Pre-defined contract Providing Microservice Contract verification tests Generates

slide-28
SLIDE 28

Fast and reliable feedback Less money spent on infrastructure API delivers exactly what is required Understanding dependency hell No stale stubs Easy to work in parallel

The Benefits

slide-29
SLIDE 29
  • Introducing microservices testing

challenges

  • Consumer-driven contract testing
  • verview
  • Following through the workflow

with Spring Cloud Contract

  • Advanced consumer-driven

contract concepts

Agenda

slide-30
SLIDE 30
slide-31
SLIDE 31

Spring Cloud Contract Overview

JVM-based CDC framework Spring Boot Integration Groovy Based Contract Definition Language Message-driven and HTTP-driven interactions

slide-32
SLIDE 32

Credit Card Application Feature

slide-33
SLIDE 33

Credit Card Application Feature

JUnit Test Credit card applications service Credit score service

slide-34
SLIDE 34

Credit Card Application Feature

JUnit Test Credit card applications service Credit score service

Applies for credit card

slide-35
SLIDE 35

Credit Card Application Feature

JUnit Test Credit card applications service Credit score service

Calculates credit score

slide-36
SLIDE 36

Credit Card Application Feature

JUnit Test Credit card applications service Credit score service

Accepts or rejects based

  • n credit score
slide-37
SLIDE 37

Following the CDC Workflow

Write a failing test Define Contract Build stubs Make tests pass Build verification tests Make tests pass

slide-38
SLIDE 38

Following the CDC Workflow

Write a failing test

slide-39
SLIDE 39

A Failing Test

slide-40
SLIDE 40

A Failing Test

slide-41
SLIDE 41

A Failing Test

slide-42
SLIDE 42

Implementing Our Endpoint

JUnit Test

slide-43
SLIDE 43

JUnit Test Credit card applications service

Implementing Our Endpoint

slide-44
SLIDE 44

JUnit Test Credit card applications service Credit score service

Does not exist

Implementing Our Endpoint

slide-45
SLIDE 45

JUnit Test Credit card applications service Credit score service

Implementing Our Endpoint

Fails here

slide-46
SLIDE 46

Following the CDC Workflow

Write a failing test Define Contract

slide-47
SLIDE 47

Contract Definition Language

Request made by our test Response expected by our test

slide-48
SLIDE 48

Following the CDC Workflow

Write a failing test Define Contract Build stubs

slide-49
SLIDE 49

Stub Generation

Contract Spring Cloud Contract Plugin Stub Jar

slide-50
SLIDE 50

wiremock.org

slide-51
SLIDE 51

Following the CDC Workflow

Write a failing test Define Contract Build stubs Make tests pass

slide-52
SLIDE 52

The Stub Runner

Download given stubs Boots WireMock Imports stubs into Wiremock

slide-53
SLIDE 53

From a Failing Test

Unit test Credit card applications service Credit score service Fails here

slide-54
SLIDE 54

…to a Passing Test

Unit test Credit card applications service Spring Cloud Contract Stub Passes here

slide-55
SLIDE 55

Following the CDC Workflow

Write a failing test Define Contract Build stubs Make tests pass Build verification tests

slide-56
SLIDE 56

Provider Test Generation

Contract Spring Cloud Contract Plugin Verification Tests

slide-57
SLIDE 57
slide-58
SLIDE 58
slide-59
SLIDE 59
slide-60
SLIDE 60

Following the CDC Workflow

Write a failing test Define Contract Build stubs Make tests pass Build verification tests Make tests pass

slide-61
SLIDE 61

From Failing…

Contract Verification Tests Credit score service

slide-62
SLIDE 62

To Passing…

Contract Verification Tests Credit score service Implement me

slide-63
SLIDE 63
  • Marcin Grzejszczak

“Consumer-driven contracts are like TDD applied at the API level.”

slide-64
SLIDE 64
  • Introducing microservices testing

challenges

  • Consumer-driven contract testing
  • verview
  • Following through the workflow

with Spring Cloud Contract

  • Advanced consumer-driven

contract concepts

Agenda

slide-65
SLIDE 65

Does this only work with exact comparisons?

slide-66
SLIDE 66

Regex JSON XML XPath Subsets Starts with Ignore casing More

slide-67
SLIDE 67

What about messaging?

slide-68
SLIDE 68

Message-driven CDC

Consumer Provider

slide-69
SLIDE 69

Message-driven CDC

Consumer Provider Broker

slide-70
SLIDE 70

Why Messaging?

Message schemas and queues are shared Asynchronous failures are a debugging nightmare! Brokers are slow

slide-71
SLIDE 71

Where do we store our contracts?

slide-72
SLIDE 72

Contract Repository

slide-73
SLIDE 73

Contract Repository

slide-74
SLIDE 74

Talk Summary

Don’t build and test microservices like a monolith Consumer-driven contracts promote independence Leverage frameworks like Spring Cloud Contract and Pact

slide-75
SLIDE 75

https://www.pluralsight.com/profile/author/andrew-morgan

Questions?

@mogronalol