Slide 1

Slide 1 text

Containers for Real Integration Tests Philipp Krenn̴̴̴@xeraa

Slide 2

Slide 2 text

Developer

Slide 3

Slide 3 text

Tests

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Integration Tests

Slide 13

Slide 13 text

Mocks

Slide 14

Slide 14 text

Mockito, EasyMock, JMock,...

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

https://www.monkeyuser.com/2018/ happy-flow/

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

In-Memory

Slide 20

Slide 20 text

H2, HSQLDB, Apache Derby,...

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

! Often good enough ! Test real datastore, supported systems

Slide 24

Slide 24 text

Actual Datastore

Slide 25

Slide 25 text

Local installation Docker container Cloud

Slide 26

Slide 26 text

Demo

Slide 27

Slide 27 text

! "Good old approach" ! External dependency, parallelization

Slide 28

Slide 28 text

Embedded

Slide 29

Slide 29 text

embedded-elasticsearch https://github.com/allegro/embedded-elasticsearch

Slide 30

Slide 30 text

Demo

Slide 31

Slide 31 text

Customization & Mappings .withPlugin("analysis-stempel") .withIndex("cars", IndexSettings.builder() .withType("car", getSystemResourceAsStream("car-mapping.json"))

Slide 32

Slide 32 text

! IDE support, customization, custom lifecycle ! Custom integration

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Build Tool

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Dockerfile or Docker assembly java:8 docker-assembly.xml java -jar /maven/service.jar

Slide 37

Slide 37 text

Build a custom image: docker:build Run container: docker:start docker:stop

Slide 38

Slide 38 text

Demo

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

! Standard or custom Docker image ! One instance for all tests, no IDE support

Slide 41

Slide 41 text

Testcontainers

Slide 42

Slide 42 text

Dependency @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.2") .withExposedPorts(6379);

Slide 43

Slide 43 text

Docker Compose integration @ClassRule public static DockerComposeContainer environment = new DockerComposeContainer(new File("src/test/resources/compose.yml")) .withExposedService("elasticsearch_1", ELASTICSEARCH_PORT, Wait.forHttp("/").forStatusCode(200));

Slide 44

Slide 44 text

Demo

Slide 45

Slide 45 text

! IDE support, customization, custom lifecycle ! Hacky configuration

Slide 46

Slide 46 text

Cleanup: Moby Ryuk https://github.com/testcontainers/moby-ryuk

Slide 47

Slide 47 text

[Ryuk] drops a Death Note, a notebook that allows the user to kill anyone simply by knowing their name and face https://en.wikipedia.org/wiki/Ryuk_(Death_Note)

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

ElasticsearchContainer https://www.testcontainers.org/modules/elasticsearch/

Slide 50

Slide 50 text

Demo

Slide 51

Slide 51 text

More languages Scala, Go, .net*, Python*, JavaScript*, Rust* * Early development

Slide 52

Slide 52 text

Selenium 2 / Webdriver VNC screen recording

Slide 53

Slide 53 text

! IDE support, customization, custom lifecycle ! Custom integration

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Docker in Docker Or sidecar

Slide 56

Slide 56 text

$ docker run -it --rm -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock maven:3 mvn --projects parent,4_testcontainers-custom test $ docker ps -a

Slide 57

Slide 57 text

Conclusion

Slide 58

Slide 58 text

Why Integration Tests

Slide 59

Slide 59 text

Why not Mocking In-Memory Actual Datastore

Slide 60

Slide 60 text

How Embedded Build Tool Testcontainers

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

Questions? Philipp Krenn̴̴̴̴@xeraa