Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Integration testing with TestСontainers and JUn...
Search
Nikolay Kuznetsov
December 09, 2019
Programming
0
160
Integration testing with TestСontainers and JUnit 5
Nikolay Kuznetsov
December 09, 2019
Tweet
Share
More Decks by Nikolay Kuznetsov
See All by Nikolay Kuznetsov
Integration testing with TestСontainers-Go
nikolayk812
0
120
Integration testing with TestContainers-Go
nikolayk812
0
220
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
310
Integration and end-to-end testing with TestСontainers
nikolayk812
0
99
Integration and end-to-end testing with TestСontainers
nikolayk812
0
19
Other Decks in Programming
See All in Programming
最近のVS Codeで気になるニュース 2025/01
74th
1
260
ISUCON14公式反省会LT: 社内ISUCONの話
astj
PRO
0
190
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
140
[Fin-JAWS 第38回 ~re:Invent 2024 金融re:Cap~]FaultInjectionServiceアップデート@pre:Invent2024
shintaro_fukatsu
0
410
Pulsar2 を雰囲気で使ってみよう
anoken
0
230
sappoRo.R #12 初心者セッション
kosugitti
0
240
AWS Lambda functions with C# 用の Dev Container Template を作ってみた件
mappie_kochi
0
240
SRE、開発、QAが協業して挑んだリリースプロセス改革@SRE Kaigi 2025
nealle
3
4.2k
Pythonでもちょっとリッチな見た目のアプリを設計してみる
ueponx
1
530
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
Grafana Loki によるサーバログのコスト削減
mot_techtalk
1
120
DROBEの生成AI活用事例 with AWS
ippey
0
130
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
The Cult of Friendly URLs
andyhume
78
6.2k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.4k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Speed Design
sergeychernyshev
26
790
Optimizing for Happiness
mojombo
376
70k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Transcript
Integration testing with TestСontainers and JUnit 5 Nikolay Kuznetsov @nikolayk812
Helsinki JUG Zalando 9 December 2019
About me • Go developer at Zalando Wardrobe • 6+
years of Java experience • Conference speaker: ◦ Voxxed Days Cluj, Container Days Hamburg • TestContainers-Go contributor
Why integration testing?
2 unit tests, 0 integration tests
Basic integration test
Trade-offs Test Runtime
Integration testing evolution • In-memory mocking • Local DBs •
Vagrant • Docker / Docker Compose • Docker API
Docker advantages • 100% compatible database • Same version as
production • Empty or known state
Docker architecture
How to start a container for test? • Shell scripts
• Maven plugin • Docker Compose • Docker API • MiniKube, Kubernetes
Shell scripts
Maven plugins github.com/fabric8io/docker-maven-plugin
Docker Compose
Docker API docs.docker.com/engine/api/latest
Exec example
None
TestContainers flavors
TestContainers Java • github.com/testcontainers/testcontainers-java • Wraps docker-java library • Docker
environment discovery • Host port randomization • Containers clean up on JVM shutdown • Readiness waiting strategies
As simple as var redis = new GenericContainer("redis:5.0.6") .withExposedPorts(6379); var
postgres = new PostgreSQLContainer();
Docker environment discovery
Talking to Docker via UDS • curl --unix-socket /var/run/docker.sock http:/localhost/containers/json
• curl --unix-socket /var/run/docker.sock http:/localhost/networks
Host port randomization • To prevent port conflicts • Enables
parallel builds • API to get a host port
Containers cleanup https://github.com/testcontainers/moby-ryuk
Waiting strategies • Host port • HTTP • Log message
• Docker healthcheck • Combined / Custom
Host port waiting strategy • Default: at first exposed port
with timeout of 60s • Both from outside and inside container
Internal port check
HTTP waiting strategy • Status & response body predicate
Demo setup User Service
Demo scenario User Service INSERT INTO SELECT FROM
Demo github.com/nikolayk812/hjug-tc-demo
Demo recap • JUnit 5 Extension API • TestContainer modules
JUnit 5 extension points • Life-cycle callbacks • Conditional execution
• Parameter resolution • Exception handling
JUnit 5 extension logic • Implement interface(s) from o.j.j.api.extension package
◦ i.e. BeforeEachCallback, ExecutionCondition • Register with @ExtendsWith annotation • See @Testcontainers for reference
TestContainers modules • Preconfigured, optimized for testing • Wrappers on
top of GenericContainer class • 14 databases • MockServer, LocalStack, Kafka, ToxiProxy
Demo-2: setup User Service
Demo-2: Docker network User Service user-alias: 8083 postgres-alias: 5432 localhost:
32812
Demo-2: scenario User Service POST /users GET /user/<id>
Demo-2 github.com/nikolayk812/hjug-tc-demo
Demo-2: recap • Docker network and alias
Why end-to-end testing? • Business flows across multiple services •
Regression, when ◦ + new service ◦ - legacy service
Some cluster Spring Cloud Kubernetes
E2E strategies • Against a deployed cluster • Against on-demand
in memory cluster
Deployed cluster cons • Replace a service with a newer
version => instability • Temporary service name => non-discoverable • Unexpected databases states ◦ Care to clear data after the test?
On-demand cluster cons • Time to start all containers •
Memory + CPU • How actually to create it?
On-demand Kubernetes for E2E?
YAGNI
On-demand cluster TC approach • Each service started by TestContainers
• Shared Docker network • Functional tests • Unless testing Kubernetes manifests
E2E setup User Service Item Service
Hints • Host port forwarding Testcontainers.exposeHostPorts() • Fixed host port
(for remote debugging) GenericContainer.addFixedExposedPort() • Reusable containers github.com/testcontainers/testcontainers-java/issues/781
Takeaways • https://testcontainers.org • Balance between flexibility, speed and features
• Works on Mac, Linux, Windows • Great for integration tests! • Possible to use for end-to-end tests
Thank you! @nikolayk812 nikolayk812 nikolayk812