Getting Started With Testing Microservices

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

Project Structure

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:

Explore technology stack

Resources (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.):

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

Project Structure

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:

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

Project Structure

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:

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?