Testen von Microservices Jrg Pfrnder Hypoport AG EUROPACE - - PowerPoint PPT Presentation

testen von microservices
SMART_READER_LITE
LIVE PREVIEW

Testen von Microservices Jrg Pfrnder Hypoport AG EUROPACE - - PowerPoint PPT Presentation

Tipps & Tricks fr das Testen von Microservices Jrg Pfrnder Hypoport AG EUROPACE EUROPACE 15% der Immobilienkredite Deutschlands EUROPACE 15% der Immobilienkredite Deutschlands ca. 3 Mrd Euro / Monat Technologien Java /


slide-1
SLIDE 1

Tipps & Tricks für das Testen von Microservices

Jörg Pfründer Hypoport AG

slide-2
SLIDE 2

EUROPACE

slide-3
SLIDE 3

EUROPACE

15% der Immobilienkredite Deutschlands

slide-4
SLIDE 4

EUROPACE

15% der Immobilienkredite Deutschlands

  • ca. 3 Mrd Euro / Monat
slide-5
SLIDE 5

Technologien

Java / Groovy MongoDB (Oracle) Spring Tomcat / Spring Boot

slide-6
SLIDE 6

Agenda

  • Traditionelle Testpyramide
  • Besondere Herausforderungen bei

Microservices

  • Postel's Law für Tests
  • Drück‘ Dich aus!
  • Production Code ist Test Code
  • Täusche und betrüge
  • Doppelt hält besser
slide-7
SLIDE 7

Monolith

slide-8
SLIDE 8
slide-9
SLIDE 9

Anzahl der Tests

slide-10
SLIDE 10

Lose Kopplung

slide-11
SLIDE 11
slide-12
SLIDE 12

#1 Postel‘s Law für Tests

slide-13
SLIDE 13

String equals

Problem #1

{„vorname“ : „Max“, „nachname“ : „Mustermann“ }

slide-14
SLIDE 14

String equals JSONassert (XMLAssert)

Problem #1

{ „vorname“ : „Max“, „nachname“ : „Mustermann“ }

slide-15
SLIDE 15

String equals JSONassert (XMLAssert) ??

Problem #1

{ „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“ }

slide-16
SLIDE 16

String equals JSONassert (XMLAssert)

Problem #1

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“ }

slide-17
SLIDE 17
  • Nur wenige Elemente der UI werden

getestet

  • Das meiste bleibt ungetestet

Inspiration: UI Automation

slide-18
SLIDE 18
  • JSONpath (Xpath)
  • Groovy RESTClient (SOAPClient)

Trick #1

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“ }

slide-19
SLIDE 19

Be conservative in what you do, be liberal in what you accept from others. Jonathan Postel RFC793

Trick #1: Postel‘s Law

Lizenziert unter Attribution über Wikimedia Commons http://commons.wikimedia.org/wiki/File:Jon_Postel.jpg#mediaviewer/File:Jon_Postel.jpg

slide-20
SLIDE 20

#2 Drück‘ Dich aus!

slide-21
SLIDE 21

Problem #2

http://geek-and-poke.com/geekandpoke/2013/7/28/tdd (Lizensiert unter Creative Commons 3.0 – mittleres Bild entfernt)

slide-22
SLIDE 22

Problem #2

http://geek-and-poke.com/geekandpoke/2013/7/28/tdd (Lizensiert unter Creative Commons 3.0 – mittleres Bild entfernt)

slide-23
SLIDE 23

Inspiration: Webdriver Page Object

slide-24
SLIDE 24

Trick #2: Response Object

  • Antwort des Services in einem

Response-Object kapseln

  • Response-Object bietet fachliche

Methoden an, macht intern die Umsetzung auf JSON bzw. XML

slide-25
SLIDE 25

Trick #2: Response Object

slide-26
SLIDE 26

Trick #2: Response Object

Verständlichkeit Wiederverwendung

slide-27
SLIDE 27

#3 Production Code ist Test Code

slide-28
SLIDE 28

Problem #3

Lose Kopplung

slide-29
SLIDE 29

Trick #3

Contract Tests

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“ }

slide-30
SLIDE 30

Trick #3: Production Code ist Test Code

Benutze die Implementierung des Clients als Test:

Consumer Driven Tests

bzw. Consumer Driven Test Suite (CDTS)

slide-31
SLIDE 31

Trick #3: Production Code ist Test Code

Consumer μ-Service Service- Provider Consumer Driven Test Suite

slide-32
SLIDE 32

Trick #3: Production Code ist Test Code

Consumer Driven Tests Technologieunabhängig?

slide-33
SLIDE 33

Trick #3: Production Code ist Test Code

beinahe technologieunabhängig:

slide-34
SLIDE 34

#4 Täusche und betrüge!

slide-35
SLIDE 35

Problem #4: Downstream Dependencies

?

μ-Service under Test Downstream Dependency

slide-36
SLIDE 36

Trick #4: Täusche und Betrüge

Mock μ-Service under Test

slide-37
SLIDE 37

#4: Mountebank - der Marktschreier

  • www.mbtest.org
  • node.js basiert
  • Erhältlich als standalone-

Installationspaket oder via npm

slide-38
SLIDE 38

μ-Service under Test Mountebank

#4: Mountebank - der Marktschreier

slide-39
SLIDE 39

POST /imposters { "port": 4545, "protocol": "http", "stubs": [ { "responses": [ { "is": { "statusCode": 200, "headers": { "Content-Type": "text/html; charset=utf-8" }, "body": "<html><body><h1>Test</h1></body></html>" } } ], "predicates": [ { "startsWith": { "path": "/dokumente/download" } } ] }

slide-40
SLIDE 40

μ-Service under Test Mountebank

#4: Mountebank - der Marktschreier

slide-41
SLIDE 41

#4: Täuscher auf Draht – Wiremock

  • www.wiremock.org
  • Java-basiert
  • als Bibliothek einbinden
  • Konfiguration als

nativer Java-Methodenaufruf

  • kein Config-Port nötig
slide-42
SLIDE 42

#4: Täuscher auf Draht – Wiremock

slide-43
SLIDE 43

Wer startet Wiremock?

Der Test Die Applikation

slide-44
SLIDE 44

Wer startet Wiremock?

Der Test

  • Mocks und Testfälle

gehören logisch zusammen

  • Separation von

Testcode und Productioncode Die Applikation

slide-45
SLIDE 45

Wer startet Wiremock?

Der Test

  • Mocks und Testfälle

gehören logisch zusammen

  • Separation von

Testcode und Productioncode

  • Kompliziertes Setup

Die Applikation

slide-46
SLIDE 46

Wer startet Wiremock?

Der Test

  • Mocks und Testfälle

gehören logisch zusammen

  • Separation von

Testcode und Productioncode

  • Kompliziertes Setup

Die Applikation

  • Weniger Wartezeiten

beim Hochfahren

  • Weniger

Konfigurations- aufwand

  • Einfacheres Arbeiten

lokal

slide-47
SLIDE 47

Trick #4: Mocking Services

www.wiremock.org www.mbtest.org (Node.js) github.com/dreamhead/moco github.com/azagniotov/stubby4j github.com/vcr/vcr (RUBY)

slide-48
SLIDE 48

#5 Doppelt hält besser!

slide-49
SLIDE 49

v3 v4 v2 v7

Problem #5: Versionierung & CD

slide-50
SLIDE 50

Problem #5: Versionierung & CD

?

μ-Service A μ-Service B μ-Service C μ-Service D

slide-51
SLIDE 51

Trick #5: Doppelt hält besser!

slide-52
SLIDE 52

Nur zwei Versionen

  • 1. Die Produktive
  • 2. Die Neue
  • > Kein Versionsmanagement
slide-53
SLIDE 53

Unbreak Changes

  • 1. Altes Schema

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“ }

slide-54
SLIDE 54

Unbreak Changes

  • 1. Altes Schema
  • 2. Eine Zwischenversion:

altes UND neues Schema

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“, „displayName“ : „Max Mustermann“ }

slide-55
SLIDE 55

Unbreak Changes

  • 1. Altes Schema
  • 2. Eine Zwischenversion:

altes UND neues Schema

  • 3. Altes Schema ausbauen

{ „id“ : 9586729475, „vorname“ : „Max“, „nachname“ : „Mustermann“, „name“ : „Max Mustermann“, „displayName“ : „Max Mustermann“ }

slide-56
SLIDE 56

Trick #5: Doppelt hält besser

Wenn doch mal versehentlich zwei Services genau gleichzeitig ausrollen, die nicht miteinander arbeiten können?

slide-57
SLIDE 57

Trick #5: Doppelt hält besser

  • Small Changesets
slide-58
SLIDE 58

Trick #5: Doppelt hält besser

  • Small Changesets
  • Echtes Continuous Deployment
slide-59
SLIDE 59

Trick #5: Doppelt hält besser

  • Small Changesets
  • Echtes Continuous Deployment
  • Miteinander Reden!
slide-60
SLIDE 60

Trick #5: Doppelt hält besser

  • Variante 1
slide-61
SLIDE 61

Trick #5: Doppelt hält besser

  • Variante 2
slide-62
SLIDE 62

Die gekippte Pyramide

slide-63
SLIDE 63

Tipps & Tricks

  • 1. Postel‘s Law:

Teste nicht zu detailliert

  • 2. Drück‘ Dich aus:

Abstrahiere mit Response-Objects

  • 3. Production Code ist Test Code:

Einfache Consumer Driven Tests

  • 4. Täusche und Betrüge:

Benutze Mocks für Downstream Dependencies

  • 5. Doppelt hält besser: CDTS in der Deployment

Pipeline

slide-64
SLIDE 64

Zum Weiterlesen

  • Mike Cohn:

http://www.mountaingoatsoftware.com/blog/the

  • forgotten-layer-of-the-test-automation-

pyramid

  • Ian Robinson: Consumer-Driven Contracts: A

Service Evolution Pattern http://martinfowler.com/articles/ consumerDrivenContracts.html

  • Brandon Byars: Enterprise Integration Using

REST http://martinfowler.com/articles/enterpriseREST .html

  • Jay Fields: Working Effectively with Unit Tests

https://leanpub.com/wewut

slide-65
SLIDE 65

Tipps & Tricks

  • 1. Postel‘s Law:

Teste nicht zu detailliert

  • 2. Drück‘ Dich aus:

Abstrahiere mit Response-Objects

  • 3. Production Code ist Test Code:

Einfache Consumer Driven Tests

  • 4. Täusche und Betrüge:

Benutze Mocks für Downstream Dependencies

  • 5. Doppelt hält besser: CDTS in der Deployment

Pipeline