Getting Started With Testing Microservices
Oleksandr Romanov
Posted on June 28, 2021
Photo by Ryoji Iwata on Unsplash
Microservices testing: from the theory to practice
In this blog post, I will share resources that help you to be more prepared to testing microservices-based systems.
My background is Java and Spring Boot. Many tools and libraries that I will share are from the Java world. But you can find analogies in other technology stacks as well.
I will split resources into the following sections - from the theory to practice:
- Learn the basics
- Explore technology stack
- Build test strategy
- Learn tools
- Investigate contract testing
Learn the basics
The first thing is to understand microservices architecture and how it differs from more traditional monolith systems.
The following resources will help you to build initial information:
- Building Microservices book by Sam Newman
- What are microservices video by freeCodeCamp
- Microservices overview by Martin Fowler
- Microservices at IBM Cloud
- The What, Why, and How of a Microservices Architecture by Jetinder Singh
Explore technology stack
Resources (Spring Boot):
- Spring Boot - project documentation
- Learn Spring Boot - Rapid Spring Application Development course on Udemy
- Spring Test library overview
- Testing WEB APIs at Spring Boot
Of you extensively use virtualization tools, like Docker - learn it.
Pay attention to how to start a single microservice in Docker container on your local machine (together with its dependencies: SQL, NoSQL databases, messaging systems, etc.):
- Advanced Functional Testing in Spring Boot Using Docker in Tests
- Easy Integration Testing With Testcontainers
If you are not from the Java world - ask your fellow developers about technologies and frameworks they use for building microservices in your organization.
Do not hesitate to ask which testing tools, practices, and guidelines they already use.
Build test strategy
Classic approach with unit, integration, and end-to-end testing can't be applied to microservices "as is." That's why approaches to testing need to be reworked and adapted.
Resources:
- Testing Strategies in a Microservice Architecture by Toby Clemson
- The Practical Test Pyramid by Ham Vocke
- Testing Microservices: an Overview of 12 Useful Techniques by Wojciech Bulaty and Liam Williams
- Testing Microservices, the sane way - very big and highly informative article by Cindy Sridharan
- Descending the Testing Pyramid: Effective Testing Strategies for Microservices slides by Chris Richardson
Learn tools
Tools and libraries can greatly simplify testing and automation and boost developer productivity.
Resources:
- Wiremock for mocking external API (API virtualization)
- Swagger for API visualization, research, and testing. Ideally, each microservices should provide an API for public and admin usage
- In-memory databases will help you to prepare service dependencies faster and make them more reliable for unit testing
- Testcontainers. The incredible library automatically runs service dependencies (databases, messaging, other services) in a separate docker containers. As a result, you will have a "real-world" setup for service component tests - but with much more control over dependencies.
- Rest Assured for API test automation. It is just one of the possible tools: do not hesitate to try something else and find a tool suitable for particular purposes
Investigate contract testing
When the number of microservices is increasing to hundreds or thousands, the problems of integration arises:
- if I deploy this API change - which microservices will I break?
- which services depend on each other?
Contract testing allows building another layer of fast and reliable tests, preventing integration issues as soon as possible in the development workflow.
Depending on the technologies used on your project, you may prefer:
- Spring Cloud Contract. It works best with Java and Spring ecosystem (but not limited to it)
- PACT tool. It supports multiple programming languages out-of-the-box
If services use gRPC protocol for communication - you can use PACT for contract testing as well.
Resources:
- What is contract testing by PACT developers
- An introduction to contract testing blog post series by Bas Dijkstra
- Introduction to contract testing with Pactflow video series by PACT developers
- Practical Contract Testing with Spring Cloud Contract - my talk at TestCon Europe
- Spring Cloud Contract course
- gRPC course
Conclusions
Microservices architecture is not a "silver" bullet in software development. The benefits also come with drawbacks.
For tester's point of view - testing microservices is like a testing system of systems: you start with single service verification in isolation, then move to integrate it with dependencies and other services, then you move to end-to-end API and UI tests as for any application.
The biggest challenge here is that the previously described testing process needs to be applied to many different services. It is not fast and scalable to do it manually - so high investment in automation is a prerequisite to any successful microservices testing effort.
This list of resources is not complete - it is just the best ones that helped me a lot in the past.
What is your favorite book or blog post about microservices?
Posted on June 28, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.