A presentation at Java Forum Nord in September 2018 in Hanover, Germany by Philipp Krenn
Philipp Krenn @xeraa Integration- Tests mit
Developer
Tests
But the unit tests passed, so... https://twitter.com/Aaronius/status/ 933497253347463168
Integration Tests
Mocks
Mockito, EasyMock, JMock,...
Ich mock mir die Welt widdewidde wie sie mir gefällt
! Unit tests, systems not under your control ! Test real datastore
In-Memory
H2, HSQLDB, Apache Derby,...
Embedded Elasticsearch unsupported in 5.0+ https://www.elastic.co/blog/elasticsearch-the-server
! Often good enough ! Test real datastore, supported systems
Actual Datastore
Local installation Docker container Cloud
Demo
! "Good old approach" ! External dependency, parallelization
Embedded
embedded-elasticsearch https://github.com/allegro/embedded-elasticsearch
Customization & Mappings .withPlugin("analysis-stempel") .withIndex("cars", IndexSettings.builder() .withType("car", getSystemResourceAsStream("car-mapping.json"))
! IDE support, customization, custom lifecycle ! Custom integration
More embedded datastores https://github.com/flapdoodle-oss/ de.flapdoodle.embed.process
Build Tool
docker-maven-plugin http://dmp.fabric8.io
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>
Build a custom image: docker:build Run container: docker:start & docker:stop
maven-failsafe-plugin https://maven.apache.org/surefire/maven-failsafe-plugin/
! Standard or custom Docker image ! One instance for all tests, no IDE support
Testcontainers
Dependency @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.2") .withExposedPorts(6379);
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));
! IDE support, customization, custom lifecycle ! Version definition, fixed config
Cleanup: Moby Ryuk https://github.com/testcontainers/moby-ryuk
[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)
testcontainers-javamodule-elasticsearch https://github.com/dadoonet/testcontainers-java-moduleelasticsearch
Work in progress https://github.com/testcontainers/testcontainers-java/pull/ 826 Set (randomized) port
More languages Scala, Go, .net , JavaScript * * Early development *
Selenium 2 / Webdriver VNC screen recording
Docker in Docker Or sidecar
$ 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
Conclusion
Why Integration Tests
Why not Mocking In-Memory Actual Datastore
How In-Process Build Tool Testcontainers
Code https://github.com/xeraa/integration-test-demo
Questions? Philipp Krenn @xeraa