Slide 1

Slide 1 text

TestContainers Integration testing without the hassle Sergei @bsideup Egorov

Slide 2

Slide 2 text

Integration testing Why it matters?

Slide 3

Slide 3 text

Verify how your software product will behave in real-world conditions

Slide 4

Slide 4 text

Isolated from other system components to avoid false negatives

Slide 5

Slide 5 text

Real: databases, file systems, network interfaces, …

Slide 6

Slide 6 text

Grey box testing - we know some inner details, but mostly use public APIs

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Unit testing • Simulation tests are green, yay! • Everything is mocked • DB is written by others, why should I test it?

Slide 9

Slide 9 text

Integration testing https://commons.wikimedia.org/wiki/File:Cd4007.jpg • Jeez, how did it passed the Unit testing? • Is it a smoke? @#$%! YES IT IS! • Who knew that 100w soldering gun was a bit too powerful for it?

Slide 10

Slide 10 text

System testing https://commons.wikimedia.org/wiki/File:UART_8250_Microchip.jpg • Takes a lot of time to solder • “Oh no, the power bus is too far away from my microchip!” • Interference between the components

Slide 11

Slide 11 text

Production

Slide 12

Slide 12 text

Integration testing Real-world, but isolated testing Spot the issues before the real environment Can be run during the development You have to start real databases Should be cross-platform Slower than Unit testing Pros Cons

Slide 13

Slide 13 text

Obvious solution

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Abstraction layer

Slide 16

Slide 16 text

CI friendly

Slide 17

Slide 17 text

Cross-platform

Slide 18

Slide 18 text

Docker Compose FTW! redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"


Slide 19

Slide 19 text

But…

Slide 20

Slide 20 text

No ports randomization redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"


Slide 21

Slide 21 text

Fighting with Docker environment

Slide 22

Slide 22 text

There is no place like

Slide 23

Slide 23 text

There is no place like Except Docker Machine

Slide 24

Slide 24 text

Can we improve that?

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Remove the database

Slide 27

Slide 27 text

Remove the database

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

As simple as PostgreSQLContainer postgresql = new PostgreSQLContainer()
 
 GenericContainer redis = new GenericContainer("redis:3")
 .withExposedPorts(6379)

Slide 30

Slide 30 text

TestContainers • https://github.com/testcontainers/testcontainers-java • OSS project by Richard North, supported by ZeroTurnaround • Declarative Docker Java API wrapper • JUnit rules, can be used with other frameworks like TestNG, Cucumber, etc…

Slide 31

Slide 31 text

How it works • Wraps https://github.com/docker-java/docker-java • Docker environment discovery • Will start docker-machine if it’s not started yet • Docker for Mac support • Containers cleanup on JVM shutdown

Slide 32

Slide 32 text

Use case #1: testing of microservices • REST service • Java, Spring Boot • Redis and PostgreSQL • Calls some other micro-services

Slide 33

Slide 33 text

Demo

Slide 34

Slide 34 text

Use case #2: Docker as Selenium driver • Selenium/Selenide tests • No need to install Chrome/Firefox/etc • CI friendly

Slide 35

Slide 35 text

Demo

Slide 36

Slide 36 text

Use case #3: Java agent testing • Test different Java versions • Simplify I/O testing • The actual reason why ZeroTurnaround decided to use TestContainers :)

Slide 37

Slide 37 text

Demo