Testcontainers and SpringBoot: from integration tests to local development

Things we love

Top things engineers wish they could spend less time on https://retool.com/reports/state-of-engineering-time-2022/

e2e Integration tests Unit tests By Unknown author - U.S. Army Photo, Public Domain, https://commons.wikimedia.org/w/index.php?curid=55124

https://labs.spotify.com/2018/01/11/testing-of-microservices/

Oleg Šelajev Developer relations @shelajev github.com/shelajev oleg@atomicjar.com

https://martinfowler.com/bliki/IntegrationTest.html

• Process isolation • FileSystem, Network • CPU/memory limits

• Con guring multiple services simultaneously • Flexibility in creating test data or use-cases • Separation of production & test environments • Dynamic con guration of external mappings • Parallel deployments fi fi 😅

—version: ‘2’ services: zookeeper: image: con uentinc/cp-zookeeper:7.0.1 ports: - “32181:32181” environment: ZOOKEEPER_CLIENT_PORT: 32181 ZOOKEEPER_TICK_TIME: 2000 kafka: image: con uentinc/cp-kafka:7.0.1 ports: - “29092:29092” depends_on: - zookeeper environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: zookeeper:32181 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:29092 fl fl KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR :1

testcontainers.org

Testcontainers-java • Created 7 years ago • github.com/testcontainers/testcontainers-java • Uses docker-java API • Integrates with frameworks, like Spring, JUnit • Automatic Docker environment discovery • Automatic cleanup

Replacing a hard problem with a hard solution is not success. Nor is replacing an easy problem with an easy solution.

System checks Checking the system… ✔ Docker version should be at least 1.6.0 ✔ Docker environment should have more than 2GB free disk space ✔ File should be mountable ℹ︎ ✔ A port exposed by a docker container should be accessible

During After

The problem Extra mile Port conflicts Port randomization Hard-coded scenarios Data-driven tests Slow tests Parallelization

We think it’s a useful default option for creating a reliable environment for running tests. … Our teams have consistently found this library of programmable, lightweight and disposable containers to make functional tests more reliable. https://www.thoughtworks.com/en-us/radar/languages-and-frameworks/testcontainers

Show me the code

Model: record Rating (String talkId, Integer value) {}

https://github.com/spring-projects/spring-boot/issues/29461

What’s next? Reusable containers Networks for more complex topologies Custom startup & waiting strategies Creating images on-the- y fl fi Logs, les, commands, limits…

What to do if you have no Docker?

Docker environments https://www.atomicjar.com/2021/10/docker-on-windows-and-macos

Testcontainers Cloud testcontainers.cloud

What’s next? — www.testcontainers.org — github.com/testcontainers/testcontainers-java ! — github.com/testcontainers/workshop

Thanks!