Testing Strategies in
Microservices
Shireesha Bongarala, Chandan Kumar
Topics
● Microservices vs Monolith
● Microservices testing
● Testing in general
What this talk is NOT about
● Security Testing
● Performance Testing
● Monitoring/ Alerting
Source: https://hackernoon.com/microservices-are-hard-an-invaluable-guide-to-microservices-2d06bd7bcf5d
A monolith is...
● ...an architecture style
● ...a way that your code
and business logic is
structured
● ...usually a single code-
base
● ...usually you can deploy
everything or nothing
Image source: https://www.slashroot.in/difference-between-monolithic-and-microservices-based-architecture
Monoliths pros/cons
● Calls are inter-process
● Simple development
● Simple testing
● Simple deployments
● Simple infrastructure
● Lack of domain boundaries
● Single technology stack
● Difficult to change
● Slow build/test times
● Slow QA process
Where monoliths break down
● Large development teams (>20 devs)
● Lack of developer discipline
● Each change means that you need a full QA
● Bugs/issues break everything
● Rollbacks cause a bottleneck
● Hard to upkeep a good internal architecture
Signs of a good monolith
● Strong, comprehensive, and quick test suite
● Requires no/minimal manual QA
● Can be deployed many times a day
● Internally structures code into modules
○ Domain Driven Design
A microservice is...
● ...small enough
● ...not too large
● ...a single domain
● ...can be deployed on its
own
● ...owns its own data
Image source: https://spring.io/blog/2015/07/14/microservices-with-spring
Microservice pros/cons
● Clear boundaries
● Smaller teams
● Deploy in isolation
● Diverse technologies
● Scalable
● Complex infrastructure
● Network calls
● Distributed systems
● Difficult integration testing
● Difficult cross-domain changes
● Difficult local development
Why testing microservices is different
● A lot of integrations to test, usually a lot of mocking
● Contract failure may not show up until UI tests
● Fewer tests for implementation detail
● Test setup on local machine is complex
Internals of a
microservice
Source: https://martinfowler.com/articles/microservice-testing/
Connections in
a microservice
Source: https://martinfowler.com/articles/microservice-testing/
Our recommendation
● Unit tests
○ Pit mutation tests
○ Property tests
● Integration tests
● Components tests
● Contract tests
● End to end tests
● Exploratory tests
Unit Tests
● Test the smallest unit of code
● Have good code coverage. How about 95%?
● Have strong assertions (avoid ‘any’)
● Use mutation tests
Source: https://me.me/i/when-you-delete-a-block-of-code-that-you-thought-13161105
Property Based Tests
Generating tests so you don’t have to!
Testing
Interactions
Source: https://martinfowler.com/articles/microservice-testing/
Microservice
datastore
Change configurations for
connecting to the test datastore
Integration Tests: Testing Repositories
Integration Tests: Testing Repositories
Source: https://www.testcontainers.org/test_framework_integration/junit_5/
Link: https://github.com/wix/wix-embedded-mysql
Microservice
(Order Service)
Mock Server
(Product service)
Change configuration to
connect to a mock server
Integration Tests: Testing External Services
Testing API Clients with Mock server
Source: http://wiremock.org/docs/getting-started/
Component Tests
Out of the process component tests, exercise the fully deployed artifact.
This can be achieved with the help of docker containers
Microservice
(Order service)
Database
(MySQL)
Mock server
(Inventory service)
Test Runner
Component Test
Contract Tests
● To verify that the contract meets the expectation by
a consuming test
● Consumer-driven contract tests
Source: https://martinfowler.com/articles/microservice-testing/
Source: https://medium.com/postman-engineering/consumer-driven-contract-testing-using-postman-f3580dba5370
Source: https://docs.pact.io/how_pact_works
End to end tests
Source:
https://martinfowler.com/articles/microservice-
testing/
End to end tests
1. Not too many
2. Test happy paths
3. Focus on personas and user journeys
Test Pyramid
Source: https://martinfowler.com/articles/microservice-testing/
A bit of advice
1. Have strong assertions
2. Keep the configuration close to production as possible
a. Use the same application.yml for testing and production and
override only where necessary
3. Keep the environment close to production as possible
a. Move from HSQL to MySQL db if your prod db is MySQL
4. The artifact is not the jar, but the docker image in microservices world
Tools/Technologies at different levels
Unit tests: Junit5, Assertions on AssertJ
Integration tests: Cucumber, Docker containers
End to end tests: TestRail, Cucumber
Contract tests: Pact
References
https://martinfowler.com/articles/microservice-testing/
https://labs.spotify.com/2015/06/25/rapid-check/
https://labs.spotify.com/2018/01/11/testing-of-microservices/
https://docs.pact.io/best_practices/consumer/contract_tests_not_functional_tests
https://reflectoring.io/7-reasons-for-consumer-driven-contracts/
https://github.com/DiUS/pact-jvm/issues/327
https://www.mabl.com/blog/understanding-contract-testing-microservices-mabl
https://bitbucket.org/atlassian/swagger-request-validator

Testing strategies in microservices

Editor's Notes

  • #2 How many are devs, QAs ? How many have experience with microservices? How many of you use Java? How many of you use Spring boot?
  • #12 Usually involves a lot of mocking
  • #30 Instead of having API producers build a specification on their own, consumers of those APIs can set expectations by letting the producers know what data they want from the API. End-to-end tests can be expensive and cumbersome to do for every change to the API.
  • #36 If you're not testing the communication between your Microservices before production, you're going to have a bad time.