Real Integration Tests with TestContainers

A presentation at ContainerDays in June 2018 in Hamburg, Germany by Philipp Krenn

Slide 1

Slide 1

Philipp Krenn @xeraa Real Integrat- 444 ion Tests with

Slide 2

Slide 2

Infrastructure | Developer !

Slide 3

Slide 3

Integration Tests

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

But the unit tests passed, so... https://twitter.com/Aaronius/status/ 933497253347463168

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

Mocking

Slide 12

Slide 12

Mockito, EasyMock, JMock,...

Slide 13

Slide 13

Slide 14

Slide 14

Ich mock mir die Welt widdewidde wie sie mir gefällt

Slide 15

Slide 15

! Unit tests, systems not under your control ! Test real datastore

Slide 16

Slide 16

In-Memory

Slide 17

Slide 17

H2, HSQLDB, Apache Derby,...

Slide 18

Slide 18

Slide 19

Slide 19

Embedded Elasticsearch unsupported in 5.0+ https://www.elastic.co/blog/elasticsearch-the-server

Slide 20

Slide 20

! Often good enough ! Test real datastore, supported systems

Slide 21

Slide 21

Actual Datastore

Slide 22

Slide 22

Local installation Docker container Cloud

Slide 23

Slide 23

Demo

Slide 24

Slide 24

! "Good old approach" ! External dependency, parallelization

Slide 25

Slide 25

In-Process

Slide 26

Slide 26

de.flapdoodle. embed.process https://github.com/flapdoodle-oss/ de.flapdoodle.embed.process

Slide 27

Slide 27

...will provide a platform neutral way for running processes in unittests.

Slide 28

Slide 28

→ MongoDB de.flapdoodle.embed.mongo → Redis de.flapdoodle.de.embed.redis → Memcached de.flapdoodle.embed.memcached → PostgreSQL ru.yandex.qatools.embed → MySQL com.wix.mysql → Consul com.github.golovnin.embedded.consul → Vault com.github.golovnin.embedded.vault → InfluxDB io.apisense.embed.influx

Slide 29

Slide 29

Build Tool

Slide 30

Slide 30

docker-maven-plugin http://dmp.fabric8.io

Slide 31

Slide 31

Build image: docker:build Run container: docker:start & docker:stop

Slide 32

Slide 32

Dockerfile or Docker assembly < build

< from

java:8 </ from

< assembly

< descriptor

docker-assembly.xml </ descriptor

</ assembly

< cmd

< shell

java -jar /maven/service.jar </ shell

</ cmd

</ build

Slide 33

Slide 33

Demo

Slide 34

Slide 34

maven-failsafe-plugin https://maven.apache.org/surefire/maven-failsafe-plugin/

Slide 35

Slide 35

! Plain or custom Docker image ! One instance for all tests

Slide 36

Slide 36

Testcontainers

Slide 37

Slide 37

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases [...] that can run in a Docker container.

Slide 38

Slide 38

Dependency @ClassRule public

static GenericContainer redis =

new GenericContainer( "redis:3.0.2" ) .withExposedPorts( 6379 );

Slide 39

Slide 39

Slide 40

Slide 40

testcontainers-java- module-elasticsearch https://github.com/dadoonet/testcontainers-java-module- elasticsearch

Slide 41

Slide 41

Demo

Slide 42

Slide 42

Todo Set (randomized) port

Slide 43

Slide 43

! Container per test Or reuse in static block ! Custom integration

Slide 44

Slide 44

Conclusion

Slide 45

Slide 45

Why Integration Tests

Slide 46

Slide 46

Why not Mocking In-Memory Actual Datastore

Slide 47

Slide 47

How In-Process Build Tool Testcontainers

Slide 48

Slide 48

Code https://github.com/xeraa/integration- test-demo

Slide 49

Slide 49

Questions? Philipp Krenn 4444 @xeraa