Slide 1

Slide 1 text

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

Slide 13

Slide 13 text

Shell scripts

Slide 14

Slide 14 text

Maven plugin github.com/fabric8io/docker-maven-plugin

Slide 15

Slide 15 text

Docker Compose

Slide 16

Slide 16 text

Docker API

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

TestContainers flavors

Slide 19

Slide 19 text

TestContainers Java ● github.com/testcontainers/testcontainers-java ● Wraps docker-java library ● Docker environment autodiscovery ● Host port randomization ● Containers clean up on JVM shutdown ● Readiness waiting strategies

Slide 20

Slide 20 text

As simple as var redis = new GenericContainer("redis:5.0.5") .withExposedPorts(6379); var postgres = new PostgreSQLContainer();

Slide 21

Slide 21 text

Docker environment autodiscovery

Slide 22

Slide 22 text

Host port randomization ● Prevents port conflicts ● Enables parallel CI builds ● API to get an actual host port

Slide 23

Slide 23 text

Containers cleanup github.com/testcontainers/moby-ryuk

Slide 24

Slide 24 text

Readiness waiting strategies ● Host port ● HTTP status/body ● Log message ● Docker healthcheck ● Combination of above ● Custom

Slide 25

Slide 25 text

Demo setup User Service

Slide 26

Slide 26 text

Demo scenario User Service INSERT INTO SELECT FROM

Slide 27

Slide 27 text

Demo github.com/nikolayk812/tu-tc-demo

Slide 28

Slide 28 text

Demo recap ● JUnit 5 integration via Extension API ○ @TestContainers / @Container ● TestContainers Modules ○ 14 databases ○ Kafka, MockServer, LocalStack, ToxiProxy, etc

Slide 29

Slide 29 text

JUnit 5 Extensions ● Customizable extension points: ○ Life-cycle phases ○ Parameter resolution ○ Conditional execution, etc ● Extension logic registered with @ExtendsWith

Slide 30

Slide 30 text

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

Slide 43

Slide 43 text

QUESTIONS ?