Failing fast to increase feedback loops
Tags: sofwaredevelopment, integrationtest, softwareengineering, codequality
Published: 2022-11-09 13:45:29

---
title: Failing fast to increase feedback loops
published: true
date: 2022-11-09 13:38:47 UTC
tags: sofwaredevelopment,integrationtest,softwareengineering,codequality
canonical_url: https://blog.igentify.com/igentify-spills-the-genes/integration-tests
---

![](https://cdn-images-1.medium.com/max/1024/0*UOFJXh1-g2mb7l5a)
<p align="center">
<figcaption>Photo by Omar Flores on Unsplash</figcaption>
</p>

#### How we used integration tests to improve confidence in our code

Picture this: your product owner brings you a set of new requirements, and together you review them. After some preparation and initial work, you get your design reviewed and approved by relevant stakeholders. Then, you enter your focus zone and start implementing it. While doing so, you want to gain confidence, so you add tons of unit tests to cover it, manually test it, and refactor your code based on your tests' results until you feel comfortable with the outcome.

![](https://cdn-images-1.medium.com/max/480/1*wxOAnUORtUJbKGD_dJdzYA.gif)

But still, you're left wondering, _"how do I get to a strong level of confidence in this code?"_

In this post, I'll discuss what I did when faced with the above situation and how my team and I improved the quality of (and our confidence in) our code.

#### Integration tests: what are they, and why do them? 🤔

There is a straightforward answer to the second part of the above question: integration tests help ensure **happy customers**.

Developers take several approaches to test their code to ensure its integrity. Choosing the suitable testing method depends on your context. Nevertheless, some tests are required for every software. These tests are represented as a layer-based model, known as the testing pyramid proposed by [Mike Cohn](https://www.mountaingoatsoftware.com/blog/the-forgotten-layer-of-the-test-automation-pyramid).

Personally, I think the image below (by [Atlassian](https://medium.com/u/5aa6b9976187)) emphasizes what we would like to achieve when choosing a specific testing approach by following each of the layers of the pyramid.

![](https://cdn-images-1.medium.com/max/800/1*LZKkD_2CaNAHjOrhfAvFJw.png)
<p align="center">
<figcaption>test pyramid</figcaption>
</p>

Hence, we chose to implement integration tests to answer the question, "Are we building the system right?"

Why did we choose integration tests? These tests determine whether the parts of the solution work together as expected. Integration tests implementation is relatively easy and can prevent errors that are hard to catch later on. In addition, integration tests help to validate builds faster, reduce the time to ship, avoid human error, adhere to continuous integration (CI) practices, and minimize cost. As a result, everyone is happier in the long run because bugs are detected earlier, and larger problems are avoided. This increases our confidence and results in higher-quality code.

#### How did we build them? 👷🏻‍♀️👷🏻‍♂️

At [Igentify](https://www.igentify.com/), our services are deployed as [Docker](https://www.docker.com/) containers. In a nutshell, containers isolate apps from their environment, solving the "it works on my machine" problem. Docker has a powerful [API](https://docs.docker.com/engine/api/), which makes it easy to automate its setup and deployment.

We use the famous [Testcontainers](https://www.testcontainers.org/) library that provides lightweight, throwaway instances of anything that can run in a container. The Testcontainers library is test-friendly, open-source, and supports [multiple programming languages](https://github.com/testcontainers). Since Igentify's services are being deployed as containers, we already have a Dockerfile, and Testcontainers simply create a new temporary image for it on-the-fly.

Head to [testcontainers](https://www.testcontainers.org/) website to find out more.

If you've read this far, you're likely interested in diving into more technical details 🤓

As an example, if the design of your system is:

- Service A is a container that talks to the "world" using a message broker in the form of [RabbitMQ](https://www.rabbitmq.com/)
- It gets a message from the RQ\_Queue , processes it, requests a pre-signed s3 URL, and stores its result in the bucket using that URL
- When finished, it responds to RS\_Queue with the output

![](https://cdn-images-1.medium.com/max/1024/0*Km1_M5XR5itK-7EH)
<p align="center">
<figcaption>Service A Design</figcaption>
</p>

At a high level, the RabbitMQ container is being replaced by TestContainers' [RabbitMQ module](https://www.testcontainers.org/modules/rabbitmq/), and its configuration is being filled by Service A context, which is being initiated by [@SpringBootTest](https://www.baeldung.com/spring-boot-testing#integration-testing-with-springboottest) annotation. The blob service is essentially a web server that gets requests via REST calls. As part of its API, the blob service generates pre-signed URLs for uploading to cloud storage, which in our case, is an AWS S3 bucket.

So, how can a web server be controlled in a test environment? API mocking. In practice, it means you replace the real implementation with a local web server for your testing purposes. We use a well-known library named WireMock, where you simply mock the various requests and responses, and you are able to instruct storing files directly to your local drive, bypassing the cloud storage. Two birds with one stone.

Find more information about Wiremock's [flexible API mocking](https://wiremock.org/).

The final piece of the puzzle is to add a consumer for the RS\_Queue to get the response and use JUnit5 to assert the expected output, resulting in a self-contained environment for testing. The input should be meticulously selected to cover success and\or failure scenarios.

Eventually, the integration test diagram looks like this:

![](https://cdn-images-1.medium.com/max/1024/0*KN5N-dfQWMtdfnc-)
<p align="center">
<figcaption>Service A integration tests</figcaption>
</p>

The last step was incorporating the integration tests into our continuous integration in the [Jenkins](https://www.jenkins.io/) server. Currently, we run it every build, but in the future, we will trigger it once a branch is merged into the main (current) branch. That will help us gain certainty that we didn't break anything.

![](https://cdn-images-1.medium.com/max/430/0*h3evkTl-lK7eWbkR.jpg)
<p align="center">
<figcaption>A Jenkins run</figcaption>
</p>

#### Final Thoughts

In this post, we reviewed how we've designed integration tests for our Java Spring Boot application with the help of TestContainers and WireMock libraries. We showed how we could easily interact with actual data while being as close as possible to the product, giving us confidence in our code.

I believe these libraries bring a lot of immediate value with a rich set of functionalities. I encourage you to give them a try. Overall, integration tests are a crucial part of any software development, as they help to ensure a more robust product, resulting in happier customers.

We used integration tests while building version 7 of our [Igentify digital genetic platform](http://www.igentify.com), which we released last month. We plan to expand the capabilities of these tests in future releases of our software. If you're interested in learning more, [please let us know here](https://info.igentify.com/en/general).