How to Create Simple Multi-Stacks Test With Testcontainers

How to Create Simple Multi-Stacks Test With Testcontainers hackernoon.com2 years ago in #Dev Love32

@arie.pratama.sArie Pratama Sutiono Senior Software Engineer | ex Data Science Manager Back-end engineering nowadays may require the integration of multiple services. It is painful for engineers to install many services in their local development environment. Docker has provided an easier way to do this, but it will still require some scripting outside our code. It also has not exactly had a perfect solution if we wanted to test smaller functions or classes instead of the whole service. This problem has been addressed by Testcontainers [1]. This article will give you an understanding of what is Testcontainers, why should you use Testcontainers, and finally how to use it. While this article will only be based on experience on Java programming language, Testcontainers does has support for other programming languages, like go [2] and node-js [3]. What is Testcontainers Testcontainers is a lightweight library that “wrap” docker and connect to test frameworks, such as Junit and Spock. It is also modular, as you can see in their maven repositories. This means, you can import only the necessary stacks that matter to you. Why Should You Use Testcontainers Testcontainers will definitely make software engineer life simpler!  Docker and Docker Compose have made deploying service in local development and production environments simpler. They are like lightweight virtual machine, thus whatever you do in your current environment will not affect services contained in docker environment. However you will need to tell docker run the service outside of your currently opened IDE. Using Tescontainers will make this even shorter. You can use ctrl F10 directly in Intellij IDEA. Another interesting aspect that makes Testcontainers shines is, it will create disposable instances. It means you can write your test with assumption that each test can be executed independently. You probably do not want to retain whatever inserted into your PostgreSQL database in one test. Retaining that test records may cause another test failing. This causing your tests having a bug. Wait! are you testing so that your main codes are bug-free?? Testcontainers will enable us to use multiple tech stacks in one test class. For instance, you need to test a Kafka consumer that does insert to a Mysql instance. We can define both services in the same class! How to use Testcontainers Testcontainers is best to be used in an integration test, although it can run within Junit runtime. Typically you wanted to run one unit test class in a few hundreds milliseconds. Running many instances of Testcontainers in a unit tests will result in a significantly slow unit test, and you do not want to run ~ 20s per unit test. It Takes Few Lines Let say you wanted to use Kafka, you can easily define your Kafka instance in your test file as public class JavaKafkaTest { @Rule public KafkaContainer kafka; @Before public void setUp() throws Exception { kafka = new KafkaContainer(“5.4.2”); kafka.start(); } } This code will download Confluent platform’s docker image file with version 5.4.2 (Kafka version 2.5.0 ) and start the docker, all in few lines of codes. Kafka is coupled with zookeeper, however, by default it has been handled by Testcontainers. If you wish to use your own zookeeper for some reason, then luckily there’s an option for it. Full Example: Producing Messages and Consuming with Kafka Streams I have shown you a little on how to make a KafkaContainer instance,  » Read More

Like to keep reading?

This article first appeared on If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply