Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5 Tipps and Tricks on Testing Microservices

5 Tipps and Tricks on Testing Microservices

Presentation at Berlin QA Meetup: http://www.meetup.com/de/Berlin-QA-Because-blame-doesnt-fix-bugs/events/226157475/
Content:
- How to build service tests that need no maintenance on non breaking API changes
- How to mock downstream dependencies
- How to build Consumer Driven Tests with almost no costs
- How to build deployment pipelines for a microservice architecture

Jörg Pfründer

November 05, 2015
Tweet

More Decks by Jörg Pfründer

Other Decks in Programming

Transcript

  1. Agenda • Test Pyramid • Microservices' Challenges • Postel's Law

    for Tests • Express yourself! • Production Code is Test Code • Mock and cheat • Double Check
  2. UI Tests → many changes → timing issues → flaky

    & brittle → high maintenance costs http://www.infoq.com/presentations/integration-tests-scam
  3. Microservices Split large software into small pieces that can be

    deployed independently → fast development → fast deployment → fast feedback → fast bugfixing
  4. String equals JSONassert (XMLAssert) ?? Problem #1 { "firstname" :

    "John", "surname" : "Doe", "name" : "John Doe" }
  5. String equals JSONassert (XMLAssert) Problem #1 { "id" : 9586729475,

    "firstname" : "John", "surname" : "Doe", "name" : "John Doe" }
  6. • Only very few elements of the UI are tested

    • Most parts of the UI untested Inspiration: UI Automation
  7. • JSONpath (Xpath) • Groovy RESTClient (SOAPClient) Trick #1 {

    "id" : 9586729475, "firstname" : "John", "surname" : "Doe", "name" : "John Doe" }
  8. 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
  9. Trick #2: Response Object • Encapsulate the service's answer into

    a response object • The response object should provide methods that represent the business perspective • The response object implements these methods internally using the JSON or XML structure
  10. Trick #2: Response Object assert userRequest.isSuccessful() == true def response

    = userRequest.response assert response.getFirstName() == "John" assert response.getSurname() == "Doe"
  11. Trick #3 Contract Tests { "id" : 9586729475, "firstname" :

    "John", "surname" : "Doe", "name" : "John Doe" }
  12. Trick #3: Contract Tests Formal specification of the contract by

    DSL • Pact https://github.com/realestate-com-au/pact • Pacto https://github.com/thoughtworks/pacto
  13. Trick #3: Production Code is Test Code Use the client's

    implementation code for tests: Consumer Driven Tests in a Consumer Driven Test Suite (CDTS)
  14. Trick #3: Production Code is Test Code Almost technology agnostic:

    #!/bin/bash waitUntilSUTstarted curl ­s "$TEST_ARTIFACT_URL" > "cdts.jar" java $PROPERTIES ­jar cdts.jar RESULT=$? rm ­f cdts.jar if [ "$RESULT" ­ne "0" ]; then error "Contract tests failed!!!" fi
  15. 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" } } ] }
  16. #4: Wiremock • www.wiremock.org • java technology • just embed

    the library in your project • configuration via native java method calls • no config port
  17. Who should start Wiremock? The Test • Mocks and test

    cases are a logical unit • Separation of test code and production code The Application
  18. Who should start Wiremock? The Test • Mocks and test

    cases are a logical unit • Separation of test code and production code • Complicated setup The Application
  19. Who should start Wiremock? The Test • Mocks and test

    cases are a logical unit • Separation of test code and production code • Complicated setup The Application • Faster startup • Less configuration • Easier local development
  20. Unbreak Changes 1. Old schema { "id" : 9586729475, "firstname"

    : "John", "surname" : "Doe", "name" : "John Doe" }
  21. Unbreak Changes 1. old schema 2. compatibility version that supports

    the old AND new schema { "id" : 9586729475, "firstname" : "John", "nachname" : "Doe", "name" : "John Doe", "displayName" : "John Doe" }
  22. Unbreak Changes 1. old schema 2. compatibility version that supports

    the old AND new schema 3. remove support for old schema { "id" : 9586729475, "firstname" : "John", "nachname" : "Doe", "name" : "John Doe", "displayName" : "John Doe" }
  23. Trick #5: Double check - the future Docker helps here:

    - standardized deployment - standardized environment
  24. Tipps & Tricks 1. Postel‘s Law: No detailed tests on

    high level 2. Express yourself: Abstractions with response objects 3. Mock and Cheat: Use test doubles for downstream dependencies 4. Production Code is Test Code: Simple consumer driven tests 5. Double Check: CDTS in the deployment pipeline
  25. Literature • 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 • Sam Newman: Building Microservices