Testing microservices with consumer-driven …...•Introducing microservices testing challenges •...

75
Testing Java Microservices with Consumer-driven contracts Andrew Morgan @mogronalol

Transcript of Testing microservices with consumer-driven …...•Introducing microservices testing challenges •...

Page 1: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Testing Java Microservices with Consumer-driven contracts

Andrew Morgan @mogronalol

Page 2: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Microservices testing is hard

Page 3: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Consumer driven contract testing

Page 4: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Andrew Morgan @mogronalol

• Independent Consultant specialising in microservices & continuous delivery

• PluralSight Author • InfoQ Editor

About Me

Page 5: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

• Introducing microservices testing challenges

• Consumer-driven contract testing overview

• Following through the workflow with Spring Cloud Contract

• Advanced consumer-driven contract concepts

Agenda

Page 6: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

• Introducing microservices testing challenges

• Consumer-driven contract testing overview

• Following through the workflow with Spring Cloud Contract

• Advanced consumer-driven contract concepts

Agenda

Page 7: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Payments

Accounts

Loans

What are Microservices?

Rewards

Page 8: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

HTTP

HTTP

Payments

Accounts

Loans

What are Microservices?

Rewards

Page 9: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

MessagingHTTP

HTTP

Payments

Accounts

Loans

What are Microservices?

Rewards

Page 10: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 11: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

The root cause of testing challenges

Payments

REST API

Booking

Page 12: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

End-to-end Testing Microservices

Automated tests Browser or other UI Environment

Page 13: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Build time

Debuggability

Infrastructure cost

Operations

Flakiness

Distributed monolith

Disadvantages of End-to-end Testing

Page 14: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Automated tests Microservice Mock or Stub of Dependency

Mocking or Stubbing Dependencies

Page 15: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

False positives

Staleness

Difficult to maintain

Disadvantages of Mocking or Stubbing

Page 16: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

The Weak Feedback Loop

Run individual microservice

testsBuild and deploy

to stage End-to-end test

False positives

Release

Slow and flaky

Page 17: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 18: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

• Introducing microservices testing challenges

• Consumer-driven contract testing overview

• Following through the workflow with Spring Cloud Contract

• Advanced consumer-driven contract concepts

Agenda

Page 19: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

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

Page 20: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

???

Test Architecture Overview

Consumer Provider

Page 21: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 22: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Key Contract Characteristics

An agreed interaction between microservices

Continuously tested

No specific protocol

Not the same stubbing!

Definitely not API documentation!

Page 23: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

What is an Interaction?

Scenario:

Should return no flights between London and Paris

Page 24: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

What is an Interaction?

Scenario:

Should return no flights between London and Paris

Required Request:

GET /flights?from=London&to=Paris

Page 25: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

What is an Interaction?

Scenario:

Should return no flights between London and Paris

Required Request:

GET /flights?from=London&to=Paris

Given Response:

200 []

Page 26: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

The Consumer Side

Automated tests

Consuming Microservice

Stub of provider microservice

Generates

The interaction our test depends on

Page 27: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

The Provider Side

Pre-defined contract

Providing Microservice

Contract verification tests

Generates

Page 28: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

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

Page 29: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

• Introducing microservices testing challenges

• Consumer-driven contract testing overview

• Following through the workflow with Spring Cloud Contract

• Advanced consumer-driven contract concepts

Agenda

Page 30: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 31: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Spring Cloud Contract Overview

JVM-based CDC framework

Spring Boot Integration

Groovy Based Contract Definition Language

Message-driven and HTTP-driven interactions

Page 32: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Credit Card Application Feature

Page 33: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Credit Card Application Feature

JUnit Test Credit card applications service

Credit score service

Page 34: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Credit Card Application Feature

JUnit Test Credit card applications service

Credit score service

Applies for credit card

Page 35: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Credit Card Application Feature

JUnit Test Credit card applications service

Credit score service

Calculates credit score

Page 36: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Credit Card Application Feature

JUnit Test Credit card applications service

Credit score service

Accepts or rejects based on credit score

Page 37: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Build stubs

Make tests pass

Build verification

tests

Make tests pass

Page 38: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Page 39: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

A Failing Test

Page 40: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

A Failing Test

Page 41: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

A Failing Test

Page 42: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Implementing Our Endpoint

JUnit Test

Page 43: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

JUnit Test Credit card applications service

Implementing Our Endpoint

Page 44: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

JUnit Test Credit card applications service

Credit score service

Does not exist

Implementing Our Endpoint

Page 45: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

JUnit Test Credit card applications service

Credit score service

Implementing Our Endpoint

Fails here

Page 46: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Page 47: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Contract Definition Language

Request made by our test

Response expected by our test

Page 48: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Build stubs

Page 49: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Stub Generation

Contract Spring Cloud Contract Plugin

Stub Jar

Page 50: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

wiremock.org

Page 51: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Build stubs

Make tests pass

Page 52: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

The Stub Runner

Download given stubs

Boots WireMock

Imports stubs into Wiremock

Page 53: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

From a Failing Test

Unit test Credit card applications service

Credit score service

Fails here

Page 54: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

…to a Passing Test

Unit test Credit card applications service

Spring Cloud Contract Stub

Passes here

Page 55: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Build stubs

Make tests pass

Build verification

tests

Page 56: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Provider Test Generation

Contract Spring Cloud Contract Plugin

Verification Tests

Page 57: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 58: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 59: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with
Page 60: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Following the CDC Workflow

Write a failing test

Define Contract

Build stubs

Make tests pass

Build verification

tests

Make tests pass

Page 61: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

From Failing…

Contract Verification Tests

Credit score service

Page 62: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

To Passing…

Contract Verification Tests

Credit score service

Implement me

Page 63: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

- Marcin Grzejszczak

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

Page 64: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

• Introducing microservices testing challenges

• Consumer-driven contract testing overview

• Following through the workflow with Spring Cloud Contract

• Advanced consumer-driven contract concepts

Agenda

Page 65: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Does this only work with exact comparisons?

Page 66: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Regex

JSON

XML

XPath

Subsets

Starts with

Ignore casing

More

Page 67: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

What about messaging?

Page 68: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Message-driven CDC

Consumer Provider

Page 69: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Message-driven CDC

Consumer ProviderBroker

Page 70: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Why Messaging?

Message schemas and queues are shared

Asynchronous failures are a debugging nightmare!

Brokers are slow

Page 71: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Where do we store our contracts?

Page 72: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Contract Repository

Page 73: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Contract Repository

Page 74: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

Talk Summary

Don’t build and test microservices like a monolith

Consumer-driven contracts promote independence

Leverage frameworks like Spring Cloud Contract and

Pact

Page 75: Testing microservices with consumer-driven …...•Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with

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

Questions?

@mogronalol