TestContainers + JUnit 5 =
elegant integration and e2e
tests for microservices
Nikolay Kuznetsov
@nikolayk812
TESTING UNITED
Vienna, 7 November 2019
Slide 2
Slide 2 text
About me
● Go developer at Zalando Helsinki
● Java developer at Infobip, DevExperts
● C developer at Samsung, Motorola
Slide 3
Slide 3 text
My testing experience
● Unit, integration and end-to-end
automated tests development
● Various organizational structures
○ Separate QA teams
○ QA engineers in a dev team
○ No QA teams/engineers at all
Slide 4
Slide 4 text
Key points
● Targeting automated testing
● Examples and demos in Java:
TestContainers-Java, JUnit 5, Spring Boot
● Principles are language-agnostic
Slide 5
Slide 5 text
Why integration testing?
Slide 6
Slide 6 text
2 unit tests, 0 integration tests
Slide 7
Slide 7 text
Tests trade-offs
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
Basic integration test
Slide 10
Slide 10 text
Integration testing evolution
● In-memory mocking
● Local DBs
● Vagrant
● Docker, Docker Compose
● Docker API
Slide 11
Slide 11 text
Docker architecture
Slide 12
Slide 12 text
How to start a container for test?
● Shell scripts
● Maven plugin
● Docker Compose
● Docker API
● MiniKube, Kubernetes
github.com/nikolayk812/tu-tc-demo
Demo-2: setup
User Service
Slide 31
Slide 31 text
Demo-2: scenario
User Service
POST /users
GET /user/
Slide 32
Slide 32 text
Demo-2: Docker network
User Service
user-alias: 8083 postgres-alias: 5432
localhost: 32812
Slide 33
Slide 33 text
Demo 2
github.com/nikolayk812/tu-tc-demo
Slide 34
Slide 34 text
Demo-2: recap
● Docker network and alias
Slide 35
Slide 35 text
Why end-to-end testing?
● Business flows across multiple services
● Regression, when
○ + new service
○ - legacy service
Slide 36
Slide 36 text
Testing/staging deployed cluster
Spring Cloud
Kubernetes
Slide 37
Slide 37 text
Testing against a deployed cluster
● Deploy a new service version to the cluster?
● Unexpected versions of dependent services
● Unexpected database states
○ Care to clear after the test?
● Parallel CI builds?
Slide 38
Slide 38 text
On-demand cluster for E2E tests
+ Locally and at CI machines
+ Control over dependent services versions
- Time to start all containers
- Resources: memory and CPU
How to do it?
Slide 39
Slide 39 text
Kubernetes for E2E tests?
Slide 40
Slide 40 text
Kubernetes for E2E tests - YAGNI
TestContainers + Docker network!
Slide 41
Slide 41 text
E2E test setup
User Service
Item Service
Slide 42
Slide 42 text
Takeaways
● https://testcontainers.org
● Balance between flexibility, speed and features
● Works on Mac, Linux, Windows