jfokus 2016 testing microservices · integration testing – things to consider september 6, 2013...

45
Jfokus 2016 Testing Microservices Katherine Stanley

Upload: others

Post on 03-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Jfokus 2016Testing Microservices

Katherine Stanley

Page 2: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

2 IBM Confidential

Introduction

September 6, 2013

Software Engineer

WebSphere Application Server Liberty

IBM

https://github.com/katheris

@KateStanley91

Page 3: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

3

Agenda

September 6, 2013

Background

What are microservices?

Example application architecture

Testing strategies

Evolving a monolithic application

Conclusion

Page 4: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

4

Background

September 6, 2013

Liberty Starter – tool to help people get started

Page 5: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

5

Out of scope for this presentation

September 6, 2013

● Performance testing

● Testing for scalability

● Security testing

● Monitoring

Page 6: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

6

What are microservices?

September 6, 2013

Page 7: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

7

What are microservices

September 6, 2013

An application that is split down into different services:

Each service has a different role

The services communicate over language-agnostic protocol

e.g. REST

The services are independently deployable

Monolithic Microservices

Page 8: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

8

Why?

September 6, 2013

To get rapid delivery

To better use scaling resources

To move pieces to the cloud – evolving a monolith

Considerations:

Increased deployment overhead

Increased complexity

Testing strategy may need changing

Page 9: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

9

Our sample microservice

September 6, 2013

Page 10: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

10

Sample microservice – anatomy

September 6, 2013

Tool to provide starter code for java applications

Split down into different technology services

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Zip Service

Application

Page 11: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

11

Sample microservice – anatomy

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Zip Service

Page 12: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

12

Testing strategies

September 6, 2013

Page 13: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

13

Testing strategies

September 6, 2013

● Unit tests

● Component tests

● Contract tests

● Integration tests

● End-to-end tests

Page 14: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

14

Testing strategies - unit

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 15: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

15

Unit testing

September 6, 2013

● Testing a small piece of behavior – usually class level

● Drives implementation when using test driven development

● Two types:

● Black box

● Test doubles (often called mocks)

Page 16: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

16

Unit testing – black box

September 6, 2013

● Use the actual objects and treat the unit as a black box

● For testing domain logic that is highly state-based

● e.g.

● Test base project construction

Page 17: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

17

Unit testing – test doubles

September 6, 2013

● Use test doubles to isolate the unit

● Useful for:

● Routing layer

● Gateway and repository testing

Page 18: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Unit testing – test doubles

September 6, 2013

Page 19: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Unit testing – test doubles

September 6, 2013

Page 20: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Unit testing

September 6, 2013

● Smaller services → more plumbing and coordination

● Unit tests can constrain implementation

● Trade off between time to maintain and usefulness

● Keep test suite small and focused

Page 21: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

21 IBM Confidential

Testing strategies - component

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 22: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

22 IBM Confidential

Component testing

September 6, 2013

● Tests a portion of the system

● Component should be a replaceable piece

● In microservices – components are services

● Two options:

● Alter the internals

● Create a 'test service'

Page 23: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

23 IBM Confidential

Component testing – altering the internals

September 6, 2013

● Use internal interfaces to aid with testing

● Does not touch the network

● Fast execution, fewer moving parts

● Things to consider:

● A less 'clean' system

● Network specific problems could get missed

Page 24: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Service List

24 IBM Confidential

Component testing – test services

September 6, 2013

● Create a test service – on the same server, or elsewhere

● Complexity is contained within the test microservice

● Thoroughly tests network calls

● Things to consider:

● Increased execution time

Data connector TestData

Domain

logic

API Gateway

Tech Service 1

Tech Service 2

Tech Service 3

Page 25: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

25 IBM Confidential

Component testing – test services

September 6, 2013

Page 26: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

26 IBM Confidential

Testing strategies - contract

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 27: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

27 IBM Confidential

Contract testing

September 6, 2013

● Contract – an agreed set of input and output attributes

● Tests the inputs and outputs have required attributes

● Tests the boundary between your application and external services

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 28: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

28 IBM Confidential

Contract testing – organization management

September 6, 2013

● Contract tests passed to the maintainers

● Sum of contract tests = service contract

● Service contract can be used to manage changes

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 29: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

29 IBM Confidential

Testing strategies - integration

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 30: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

30 IBM Confidential

Integration testing

September 6, 2013

● Tests the interactions between components

● Test for basic success and error paths

● In microservice environment tests interaction with:

● Other services

● Data stores

Page 31: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

31 IBM Confidential

Integration testing – things to consider

September 6, 2013

● Connection failures could cause false errors

● Use unit and contract testing for behavior

● Consider moving these tests to the build pipeline

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 32: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

32 IBM Confidential

Testing strategies - end-to-end

September 6, 2013

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 33: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

33 IBM Confidential

End-to-End

September 6, 2013

● Verifies system meets external requirements

● Sanity check

● Important in stateful systems

● Include UI testing

Page 34: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

34 IBM Confidential

Testing Strategies

September 6, 2013

● Unit

Tech Service 1

Tech Service 2

Tech Service 3

Service List

Data connector

Domain

logic

API Gateway

Page 35: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Service List

35 IBM Confidential

Testing Strategies

September 6, 2013

● Unit

● ComponentTech Service 1

Tech Service 2

Tech Service 3Data connector

Domain

logic

API Gateway

Page 36: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Service List

36 IBM Confidential

Testing Strategies

September 6, 2013

● Unit

● Component

● Contract

Tech Service 1

Tech Service 2

Tech Service 3Data connector

Domain

logic

API Gateway

Page 37: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Service List

37 IBM Confidential

Testing Strategies

September 6, 2013

● Unit

● Component

● Contract

● Integration

Tech Service 1

Tech Service 2

Tech Service 3Data connector

Domain

logic

API Gateway

Page 38: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

Service List

38 IBM Confidential

Testing Strategies

September 6, 2013

● Unit

● Component

● Contract

● Integration

● End-to-End

Tech Service 1

Tech Service 2

Tech Service 3Data connector

Domain

logic

API Gateway

Page 39: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

39 IBM Confidential

Monolith → Microservice

September 6, 2013

Page 40: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

40IBM Confidential

Monolith → Microservice

September 6, 2013

● Add tests for current function – fix flaws later

● The monolith will change, so will your tests

● Useful tools:

● Minimal set of externally calling classes

● Mocking for unit test

● Use a 'test' service for component tests

Page 41: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

41 IBM Confidential

Conclusion

September 6, 2013

Page 42: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

42 IBM Confidential

Conclusion

September 6, 2013

● Still a developing area

● For evolution take small steps

● Dummy service useful tool

● Adapt tests to suit the development and deployment process

Page 43: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

43 IBM Confidential

Questions?

http://martinfowler.com/articles/microservice-testing/

wasdev.net → Docs → Microservices

ibm.biz/LibertyStarter

@KateStanley91

September 6, 2013

Page 44: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

44 IBM Confidential

Testing in maven and gradle

September 6, 2013

Maven

Structured

Transferable skill

Gradle

More freedom

Risk of creating a very complicated build system

Page 45: Jfokus 2016 Testing Microservices · Integration testing – things to consider September 6, 2013 Connection failures could cause false errors Use unit and contract testing for behavior

45 IBM Confidential

Testing in gradle

September 6, 2013