Testing Java Spring Boot Microservices

Tests are an essential part of our codebase. At the very least, they minimize the risk of regression when we modify our code. There are several types of tests and each has a specific role: unit tests, integration tests, component tests, contract tests and end-to-end tests. It is crucial to understand the role of each type of test in order to leverage their potential.

The goal of this article is to describe a strategy to use them in order to test Java Spring Boot microservices. For every type of test, we will try to explain its role, its scope as well as tooling we like to use.

java, microservices, spring, tests

How to Write Robust Component Tests

Component tests allow to test complete use cases from end to end. They are often expensive especially in terms of setup and execution time. Thus, thought needs to be given to define their scope. Nevertheless, they are required in order to check and document the overall behaviour of the application or the microservice.

I have noticed that, in the context of microservices, these tests are very cost-effective. Indeed, they can be quite easy to set up because the already existing external API of the microservice can often be used directly without needing additional elements (like a fake server for instance). Moreover, the scope of a microservice is generally limited and can be tested exhaustively in isolation.

The goal of this article is to show how to make these tests robust. The main idea is to make them independent of the implementation.

java, microservices, tests

Setup a Circuit Breaker with Hystrix, Feign Client and Spring Boot

In a microservices architecture, several things can go wrong. A middleware, the network or the service you want to contact can be down. In this world of uncertainty, you have to anticipate problems in order not to break the entire chain and throw an error to the end user when you could offer a partially degraded service instead.

The goal of this article is to show how to implement the circuit breaker pattern using Hystrix, Feign Client and Spring Boot.

java, microservices, spring

Refactoring Conditional Structures with Map

When working on already existing codebases, I often encounter pieces of code that look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Day {
  public void start(Weather weather) {
    switch(weather) {
      case RAINY:
          takeAnUmbrella();
          break;
      case SUNNY:
          takeAHat();
          break;
      case STORMY:
          stayHome();
          break;
      default:
          doNothing();
          break;
    }
  }
}

Basically, depending on the weather, an action has to be taken. This kind of code is pretty hard to test and to maintain. The goal of this article is to refactor it using a Map.

clean code, java, refactoring

Controlling the Time in Java

Time is a tricky thing, it’s always changing. Having such moving parts into the codebase can be very annoying when testing, for instance. In this article, we will see how to control the time in Java.

java, tests

Go Back in Time with Git

Recently, I conducted a workshop about how to go back in time with Git alongside Renaud. Here are the main points that were raised during this session.

git

Keep Calm and Use Git Reflog

Git is a very powerful tool but it takes a fair amount of time to masterize it. It can be particuarly easy to break or lose things when you amend some commits, resolve conflicts or force push your changes.

In this blog post, I will show you how to use the git reflog command to retrieve commits that you thought you lost forever.

git

Trivia Kata: A Refactoring Story

Refactoring a legacy code can be a tricky business. More often than not, the code is not tested, unclear and contains some bugs. If not planned, the time required to refactor will eat all the time allocated to the feature. The feature will then be implemented very quickly and it will result in more buggy unmaintainable code.

In this blog post, I will try to show some tools and methods to refactor safely a piece of code. I will use the trivia kata as a support for this. The resulting code can be found on GitHub.

java, refactoring

Quand faire un audit de code ?

Dans notre activité, nous sommes amenés à faire régulièrement des audits de code. Planifié ou forcé, un audit a pour but de déterminer la qualité logicielle d'une base de code et de faire des préconisations pour améliorer sa santé. L'objectif de cet article est de faire un retour d'expérience sur les différentes circonstances qui mènent à la réalisation d'un tel audit.

audit

Pourquoi utiliser le TDD est une bonne idée pour apprendre un nouveau langage

Durant ma dernière mission, j'ai eu à écrire du code dans des langages que je ne connaissais pas du tout, ou très peu (Ruby, Perl, Python, Go). J'ai donc expérimenté une technique qui consiste à apprendre un langage par les tests. Cette technique se nomme le Test Driven Learning.

L'objectif de cet article est de décrire pourquoi il peut être intéressant d'utiliser le TDD pour apprendre un nouveau langage de manière efficace.

ruby, tdd