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
180
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Integration testing with TestСontainers and JUnit 5
Nikolay Kuznetsov
December 09, 2019
More Decks by Nikolay Kuznetsov
See All by Nikolay Kuznetsov
Integration testing with TestСontainers-Go
nikolayk812
0
140
Integration testing with TestContainers-Go
nikolayk812
0
280
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
380
Integration and end-to-end testing with TestСontainers
nikolayk812
0
130
Integration and end-to-end testing with TestСontainers
nikolayk812
0
37
Other Decks in Programming
See All in Programming
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
550
AWS Transform Customによる Spring Boot 2.xから4.xへのVerUp
satoshi256kbyte
2
110
「寝てても仕事が進む」Claude Codeで組む第二の脳
tomoyafujita2016
0
360
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
630
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
110
「人を評価する AI」の設計と実装
ryoyanara
0
240
Press start. Python's next generation.
willingc
PRO
3
170
MySQLとPostgreSQLって何が違うの?
akagami
0
140
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
260
メールのエイリアス機能を履き違えない
isshinfunada
0
260
T3DD26: From RAGs to Riches
martinhelmich
0
100
ALB ログから Trace を気合で繋げる技術
fohte
6
670
Featured
See All Featured
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
220
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
790
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
180
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
240
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
500
My Coaching Mixtape
mlcsv
0
290
First, design no harm
axbom
PRO
2
1.3k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
380
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
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