Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TestContainers - integration testing without the hassle

TestContainers - integration testing without the hassle

Presented @ TopConf Tallinn

Sergei Egorov

November 16, 2016
Tweet

More Decks by Sergei Egorov

Other Decks in Programming

Transcript

  1. Unit testing • Simulation tests are green, yay! • Everything

    is mocked • DB is written by others, why should I test it?
  2. 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?
  3. 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
  4. 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
  5. Docker Compose FTW! redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:


    image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"

  6. No ports randomization redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:


    image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"

  7. As simple as PostgreSQLContainer postgresql = new PostgreSQLContainer()
 
 GenericContainer

    redis = new GenericContainer("redis:3")
 .withExposedPorts(6379)
  8. 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…
  9. 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
  10. Use case #1: testing of microservices • REST service •

    Java, Spring Boot • Redis and PostgreSQL • Calls some other micro-services
  11. Use case #2: Docker as Selenium driver • Selenium/Selenide tests

    • No need to install Chrome/Firefox/etc • CI friendly
  12. Use case #3: Java agent testing • Test different Java

    versions • Simplify I/O testing • The actual reason why ZeroTurnaround decided to use TestContainers :)