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
170
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
130
Integration testing with TestContainers-Go
nikolayk812
0
250
TestContainers + JUnit 5 = elegant integration and e2e tests for microservices
nikolayk812
2
340
Integration and end-to-end testing with TestСontainers
nikolayk812
0
110
Integration and end-to-end testing with TestСontainers
nikolayk812
0
25
Other Decks in Programming
See All in Programming
コンテキストエンジニアリング Cursor編
kinopeee
1
760
Jakarta EE Core Profile and Helidon - Speed, Simplicity, and AI Integration
ivargrimstad
0
340
HTMLの品質ってなんだっけ? “HTMLクライテリア”の設計と実践
unachang113
4
2.1k
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
1.4k
意外と簡単!?フロントエンドでパスキー認証を実現する WebAuthn
teamlab
PRO
1
370
UbieのAIパートナーを支えるコンテキストエンジニアリング実践
syucream
2
820
AI時代のUIはどこへ行く?
yusukebe
13
7.8k
速いWebフレームワークを作る
yusukebe
5
1.7k
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
410
CSC305 Summer Lecture 12
javiergs
PRO
0
130
AWS発のAIエディタKiroを使ってみた
iriikeita
1
160
詳解!defer panic recover のしくみ / Understanding defer, panic, and recover
convto
0
220
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Producing Creativity
orderedlist
PRO
347
40k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Raft: Consensus for Rubyists
vanstee
140
7.1k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
The Language of Interfaces
destraynor
161
25k
Mobile First: as difficult as doing things right
swwweet
224
9.9k
Agile that works and the tools we love
rasmusluckow
330
21k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Automating Front-end Workflow
addyosmani
1370
200k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
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