Testcontainers and SpringBoot: from integration tests to local development

A presentation at Spring IO 2022 in May 2022 in Barcelona, Spain by Oleg Šelajev

Slide 1

Slide 1

Testcontainers and SpringBoot: from integration tests to local development

Slide 2

Slide 2

Things we love

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

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

Slide 6

Slide 6

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

Slide 7

Slide 7

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

Slide 8

Slide 8

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

Slide 9

Slide 9

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

Slide 10

Slide 10

Slide 11

Slide 11

Slide 12

Slide 12

Slide 13

Slide 13

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

Slide 14

Slide 14

• 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 😅

Slide 15

Slide 15

—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

Slide 16

Slide 16

testcontainers.org

Slide 17

Slide 17

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

Slide 18

Slide 18

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

Slide 19

Slide 19

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

Slide 20

Slide 20

During After

Slide 21

Slide 21

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

Slide 22

Slide 22

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

Slide 23

Slide 23

Slide 24

Slide 24

Show me the code

Slide 25

Slide 25

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

Slide 26

Slide 26

Slide 27

Slide 27

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

Slide 28

Slide 28

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…

Slide 29

Slide 29

What to do if you have no Docker?

Slide 30

Slide 30

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

Slide 31

Slide 31

Testcontainers Cloud testcontainers.cloud

Slide 32

Slide 32

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

Slide 33

Slide 33

Thanks!