Testcontainers

Testcontainers logo

About

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.

Testcontainers make the following kinds of tests easier:

  • Data access layer integration tests: use a containerized instance of a MySQL, PostgreSQL or Oracle database to test your data access layer code for complete compatibility, but without requiring complex setup on developers' machines and safe in the knowledge that your tests will always start with a known DB state. Any other database type that can be containerized can also be used.
  • Application integration tests: for running your application in a short-lived test mode with dependencies, such as databases, message queues or web servers.
  • UI/Acceptance tests: use containerized web browsers, compatible with Selenium, for conducting automated UI tests. Each test can get a fresh instance of the browser, with no browser state, plugin variations or automated browser upgrades to worry about. And you get a video recording of each test session, or just each session where tests failed.
  • Much more! Check out the various contributed modules or create your own custom container classes using GenericContainer as a base.

Prerequisites

Maven dependencies

Testcontainers is distributed as separate JARs with a common version number:

  • A core JAR file for core functionality, generic containers and docker-compose support
  • A separate JAR file for each of the specialised modules. Each module's documentation describes the Maven/Gradle dependency to add to your project's build.

For the core library, the latest Maven/Gradle dependency is as follows:

testCompile "org.testcontainers:testcontainers:1.14.1"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>1.14.1</version>
    <scope>test</scope>
</dependency>

You can also check the latest version available on Maven Central.

Managing versions for multiple Testcontainers dependencies

To avoid specifying the version of each dependency, you can use a BOM or Bill Of Materials.

Using Maven you can add the following to dependencyManagement section in your pom.xml:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers-bom</artifactId>
            <version>1.14.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
and then use dependencies without specifying a version:

<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>mysql</artifactId>
    <scope>test</scope>
</dependency>

Using Gradle 5.0 or higher, you can add the following to the dependencies section in your build.gradle:

implementation platform('org.testcontainers:testcontainers-bom:1.14.1') //import bom
testImplementation('org.testcontainers:mysql') //no version specified

JitPack builds are available for pre-release versions.

Shaded dependencies

Testcontainers uses the docker-java client library, which in turn depends on JAX-RS, Jersey and Jackson libraries. These libraries in particular seem to be especially prone to conflicts with test code/application under test code. As such, these libraries are 'shaded' into the core testcontainers JAR and relocated under org.testcontainers.shaded to prevent class conflicts.

Who is using Testcontainers?

  • ZeroTurnaround - Testing of the Java Agents, micro-services, Selenium browser automation
  • Zipkin - MySQL and Cassandra testing
  • Apache Gora - CouchDB testing
  • Apache James - LDAP and Cassandra integration testing
  • StreamSets - LDAP, MySQL Vault, MongoDB, Redis integration testing
  • Playtika - Kafka, Couchbase, MariaDB, Redis, Neo4j, Aerospike, MemSQL
  • JetBrains - Testing of the TeamCity plugin for HashiCorp Vault
  • Plumbr - Integration testing of data processing pipeline micro-services
  • Streamlio - Integration and Chaos Testing of our fast data platform based on Apache Puslar, Apache Bookeeper and Apache Heron.
  • Spring Session - Redis, PostgreSQL, MySQL and MariaDB integration testing
  • Apache Camel - Testing Camel against native services such as Consul, Etcd and so on
  • Infinispan - Testing the Infinispan Server as well as integration tests with databases, LDAP and KeyCloak
  • Instana - Testing agents and stream processing backends
  • eBay Marketing - Testing for MySQL, Cassandra, Redis, Couchbase, Kafka, etc.
  • Skyscanner - Integration testing against HTTP service mocks and various data stores
  • Neo4j-OGM - Testing new, reactive client implementations
  • Lightbend - Testing Alpakka Kafka and support in Alpakka Kafka Testkit
  • Zalando SE - Testing core business services
  • Europace AG - Integration testing for databases and micro services
  • Micronaut Data - Testing of Micronaut Data JDBC, a database access toolkit
  • Vert.x SQL Client - Testing with PostgreSQL, MySQL, MariaDB, SQL Server, etc.
  • JHipster - Couchbase and Cassandra integration testing
  • wescale - Integration testing against HTTP service mocks and various data stores
  • Marquez - PostgreSQL integration testing
  • Transferwise - Integration testing for different RDBMS, kafka and micro services
  • XWiki - Testing XWiki under all supported configurations

License

See LICENSE.

Attributions

This project includes a modified class (ScriptUtils) taken from the Spring JDBC project, adapted under the terms of the Apache license. Copyright for that class remains with the original authors.

This project was initially inspired by a gist by Moshe Eshel.

Copyright (c) 2015-2020 Richard North and other authors.

See AUTHORS for contributors.