Integration Testing with

Docker and Testcontainers


  • Doctoral Researcher @ Institute for Internet Security
    • Head of Blockchain Lab
  • Consultant & Coach
  • Licium Co-Founder
  • Testcontainers Maintainer and Open Source Enthusiast
  • Oracle Groundbreaker Ambassador

Kevin Wittek          @kiview

How I ended up on a dairy farm to patch a JEE application...

Key learnings

  • Easy setup of complete test environments
  • Not a lot effort to integration test using real external applications
  • Avoid integrated tests!
$ git clone
$ cd bar
$ ./gradlew build

My definiton

For example:

  • Launch application server
  • Framework/Library interactions
  • Interact with external ports (e.g. network, file system, database) 

"Tests which interact with external systems/dependencies"

But why a pyramid?

Testing Honeycomb

Integrated Tests

"I use the term integrated test to mean any test whose result (pass or fail) depends on the correctness of the implementation of more than one piece of non-trivial behavior." -  J.B. Rainsberger

"A test that will pass or fail based on the correctness of another system." - Spotify

Integrated Tests

For example:

  • We (manually) spin up other services in a local testing environment
  • We test against other services in a shared testing environment
  • Changes to your system breaks tests for other systems

Which tools to use?

(in the JVM world and beyond)

Why combine?

  • Easy setup of dev environment
  • Uniform build and test environments
    • Also self contained and portable!
  • No installation and setup of external software
    • Except Docker 😜

How to fuse both worlds?

GenericContainer redis =
    new GenericContainer("redis:3.0.2")


// test my stuff


The Project

  • testcontainers-java first released in 2015
  • 100% OSS, MIT licensed
  • 65 releases, 149 contributors
  • Core maintainers
    • Richard North
    • Sergei Egorov
    • Kevin Wittek
  • Forks in Python, C#, Rust, Go, JS; Scala wrapper



  • Generic docker container support
    • use any Docker image to support tests
  • Databases (many!) and Stream processing (Kafka, Pulsar)
  • AWS mocks (Localstack)
  • Docker Compose
    • Or Docker Compose alternative!
  • Selenium
  • Chaos testing (toxiproxy)

Capabilities (2)

  • Dynamic port binding and API
  • WaitStrategies
  • Docker environment discovery (e.g. docker-machine, DOCKER_HOST, Docker for Mac, Docker for Windows)
  • Platform independent
    • Linux, macOS, Windows 10 (with NPIPE support!)


Works on Windows too!

Support all Major CI Systems!

  • Jenkins
  • TravisCI
  • GitlabCI
  • CircleCI
  • Azure DevOps Pipelines
  • Bitbucket
  • GitHub Actions
  • ...
// Set up a redis container
public static GenericContainer redis =
    new GenericContainer("redis:3.0.2")

JUnit4 Rules

Spock Extension

class TestContainersClassIT extends Specification {

    GenericContainer genericContainer = 
       new GenericContainer("postgres:latest")
                POSTGRES_USER: "foo"
                POSTGRES_PASSWORD: "secret"

Testcontainers-Jupiter (JUnit5)

class SomeTest {

    private MySQLContainer mySQLContainer = new MySQLContainer();

    void someTestMethod() {
        String url = mySQLContainer.getJdbcUrl();
         // create a connection and run test as normal

Testcontainers 1.14.0 Released!

Auto generated GHA

Auto generated GHA

Test Matrix

task testMatrix {
    project.afterEvaluate {
        def checkTasks = subprojects.collect {
        }.findAll { it != null }

        doLast {
            // Obtain a list of check tasks that are not up-to-date, i.e.
            //  the ones which Gradle cannot find a cached output for.
            def checkTaskPaths = checkTasks
                .findAll { !it.state.upToDate }
                .collect { it.path }


Spring 5.2.5


class ExampleIntegrationTests {

    static Neo4jContainer<?> neo4j = new Neo4jContainer<>();

    static void neo4jProperties(DynamicPropertyRegistry registry) {
        registry.add("", neo4j::getBoltUrl);



version: "3"

    image: vote-frontend
    command: python
     - ./vote:/app
      - "5000:80"

    image: redis:alpine
    ports: ["6379"]

    image: worker

    image: postgres:9.4

    image: result-frontend
    command: nodemon --debug server.js
      - ./result:/app
      - "5001:80"
      - "5858:5858"

TC as a DockerCompose replacement

CI inside containers?

Docker in Docker - DinD

Docker Wormhole

Docker Wormhole


and Common Pitfalls...

SELF-typing is sometimes a PITA

  • Pattern defined in "Effective Java"
    • Generic type with a recursive type paramter
    • MyClass<T extends MyClass<T>>
  • Doesn't play nice with other JVM languages (Scala, Kotlin)
  • Confusing for new users and contributors
  • Not consistent with all modules


  • Using functional Builder-Pattern
    • Maybe Lombok @SuperBuilder?
  • Double Brace Initialization
    • People don't like this 😱

Double Brace Initialization 😜

private GenericContainer myContainer = new GenericContainer("myImage:42.23") {{

   if (System.getenv().get("INSIDE_CI") == null) {


TDD workflows

  • Container startup introduces overhead
  • Testcontainers should be ephemeral
  • We still want to avoid test pollution and integrated tests

Moar Future

  • Testcontainers 2.0
  • Complete Podman support
    • Working with RedHat on making the TC test suite feature parity reference test suite :)
  • Container-Core
    • Test-Framework agnostic
    • OO abstraction of Docker containers
  • Other languages
    • More languages and aligning existing forks
    • GraalVM (mind=blown)

Far Future...

TODO: add Safe Harbor Statement here

  • Windows Containers on Windows (WCOW)
  • Orchestrators (Docker Swarm, Kubernetes)
    • Maybe one day... (Help from experts welcome!)
  • Other container engines (e.g. Podman, Firecracker)
  • Build and test inside containers with full IDE support
    • Cloud IDEs (Codespaces?)
    • Visual Studio Code Remote Development

GitHub Sponsors


Testing with Testcontainers (TestCon2020)

By Kevin Wittek

Testing with Testcontainers (TestCon2020)

  • 155
Loading comments...

More from Kevin Wittek