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

Integration Testing with TestContainers

Integration Testing with TestContainers

Ottawa Java User Group presentation. September 28, 2017.

Aleksey Vorobyov

September 29, 2017
Tweet

More Decks by Aleksey Vorobyov

Other Decks in Technology

Transcript

  1. Why integration tests are important • 1st test in Real

    World ◦ Modern [micro]service works with a lot of other services ◦ Points of integration are sources of failures • Isolation. Errors from other services don’t affect a system under test. ◦ In system tests it takes more time to understand a root cause
  2. Use real resources • Databases ◦ Embedded databases like H2

    don’t really test anything • Files • Other services (HTTP mocks) • Message queue middleware • Networking
  3. Advantages of integration tests • Real world testing • Isolated

    verification. Other services are under control. • Find issues before deployment • Can be used on a developer machine ◦ Sometimes it’s too hard to run System Tests on a developer machine
  4. Ways to run integration tests • Install services locally •

    Provision embedded services from tests • Use VM/Vagrant Disadvantages • Dev machine pollution • Hard to run/manage/cleanup tests on CI especially in parallel
  5. TestContainers library TestContainers is a Java library that supports JUnit

    tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
  6. Problems TestContainers solves • Integration tests isolation ◦ Containers/Services from

    different tests are isolated ◦ Containers use random ports on a host machine ◦ Tests can be started on the same machine in parallel without port conflicts • Cleanup containers on JVM shutdown ◦ CI servers are always in a predictable state • Hides verbose container management commands behind developer friendly tools (Java code)
  7. TestContainers use cases • Data Repository tests ◦ Start a

    database container ◦ Configure a repository datasource ◦ Test repository methods • Integration tests ◦ Start dependent services in containers ◦ Configure a service under test ◦ Test the service API • UI/Acceptance tests ◦ Provision whole System ◦ Test an application using UI/Acceptance tools
  8. How to use TestContainers @ClassRule public static GenericContainer redis =

    new GenericContainer("redis:3") .withExposedPorts(6379); Then redis.getContainerIpAddress() redis.getMappedPort(6379) returns a binded random port on localhost
  9. Example. Run inside CI node Run tests from inside another

    docker instance docker run -v /var/run/docker.sock:/var/run/docker.sock