Writing Integration Tests with Docker Compose and JUnit

1 Comment

An integration test verifies the communication paths and interactions between components to detect interface defects.

— Martin Fowler

In my current project we are building a mobility platform using a microservice architecture. Because all of our services are interacting with external componentes like e.g. DynamoDB, PostgreSQL, Apache Kafka, or etcd we started looking for a simple and efficient way to write integration tests in Java.

After a little research we came up with a minimalistic test setup that makes use of Docker Compose to spin up the dependencies, JUnit and Gradle to execute the tests, and docker-compose-rule to hold it all together.

Here is how you do it…

Let’s get started

In the following tutorial I will show you how to setup and run a simple integration test that verifies the CRUD functionality of a service using DynamoDB. You will then be able to adopt these techniques to write integration tests for your own use cases.

The entire tutorial should take no longer than 20 minutes.


In order to run the code of this tutorial on your machine you need to install Docker and Docker Compose. That is all.

The entire source code of this tutorial can be found on https://github.com/tomasulo/docker-compose-integration-tests.

Step 1: Setup your build.gradle

The first step is to setup our Gradle build. We do this by adjusting the build.gradle.

We are using docker-compose-rule in our tests to orchestrate the interaction with Docker Compose. For this we need to add their bintray repository to our `repositories` configuration and define a testCompile dependency on 'com.palantir.docker.compose:docker-compose-rule-junit4:0.31.1'.

Then we also want to separate our unit tests from our integration tests so that we can run the Gradle tasks test and integrationTest independently.

We do this by creating a second test task called integrationTest in which we will include a category (more about that later). The regular test task also needs to be adjusted to exclude the aforementioned category.

The complete build.gradle now looks like this:

plugins {
    id 'java'
    id 'idea'
repositories {
    maven {
    // docker-compose-rule is published on bintray
    url 'https://dl.bintray.com/palantir/releases'
dependencies {
    compile 'com.amazonaws:aws-java-sdk-dynamodb:1.11.86'
    compile 'org.apache.commons:commons-lang3:3.5'
    compile 'ch.qos.logback:logback-classic:1.1.10'
    testCompile 'junit:junit:4.12'
    testCompile 'org.assertj:assertj-core:3.6.2'
    testCompile 'com.palantir.docker.compose:docker-compose-rule-junit4:0.31.1'
test {
    useJUnit {
        excludeCategories 'com.tomasulo.sample.IntegrationTest'
task integrationTest(type: Test) {
    useJUnit {
        includeCategories 'com.tomasulo.sample.IntegrationTest'

Step 2: Setup your docker-compose file

The next step is to configure the docker-compose file for the external component we want our tests to interact with. In this tutorial we are spinning up an instance of DynamoDB using the docker image peopleperhour/dynamodb.

The complete configuration looks like this:

version: "3"

    image: peopleperhour/dynamodb
      - "awsRegion=EU_WEST_1"
      - "awsAccessKey=KEY"
      - "awsSecretKey=SECRET_KEY"
      - "8000"
    hostname: dynamodb

Make sure you are not exposing any ports to avoid conflicts when e.g. running the tests in a CI environment.

In order to make the docker-compose-dynamodb.yml available for our tests we will put it into the folder src/test/resources. You can then test your configuration by starting the container with docker-compose -f docker-compose-dynamodb.yml up and tearing it back down with docker-compose -f docker-compose-dynamodb-yml down.

You can find more information about the docker-compose file here: https://docs.docker.com/compose/compose-file/.

Step 3: Implementation

Now we are going to configure our tests. But before we do that you should create the category interface we previously talked about:

public interface IntegrationTest {

We can then annotate the integration tests with that category:

public class UserRepositoryIntegrationTest {

The next step is to configure the docker-compose-rule for our purpose. We are using a @ClassRule for this:

public static DockerComposeRule docker = DockerComposeRule.builder()
    .waitingForService(DYNAMODB, HealthChecks.toHaveAllPortsOpen())

This configuration will make sure that the service specified in the docker-compose file will be started before the tests and also torn down after.

More info about how to configure the docker-compose-rule can be found here: https://github.com/palantir/docker-compose-rule.

We can now use DockerComposeRule docker in our @BeforeClass to get the external port assigned from Docker to configure the DynamoDB connection:

private static UserRepository repository;
public static void initialize() {
    DockerPort dynamodb = docker.containers()
    String dynamoEndpoint = String.format("http://%s:%s", dynamodb.getIp(), dynamodb.getExternalPort());
    repository = new UserRepository(dynamoEndpoint,

That is all the configuration we need. You are now ready to implement tests and business logic.

Step 4: Profit

Then you are done. It is really that simple.

You can now run ./gradlew clean integrationTest to verify your code in seconds. Or you could simply use the regular JUnit runner of your IDE (e.g. IntelliJ) and even debug your code.

Testing integration points with external components this way really helped us to gain further confidence in our code and also allowed us to test new functionality quickly and effectively.

I hope this tutorial enables you to do the same!

I highly recommend reading https://martinfowler.com/articles/microservice-testing/if you are interested in more test-patterns for microservices!

Thomas is a developer with the mindset of a true software craftsman. He is an advocate of clean code and is always looking for opportunities to improve his skills or learn something new.

He has been with codecentric for over two years and his specialties are Microservices, Continuous Delivery, agile software development, and managing builds with Gradle.

More content about Agile Testing


  • Jasper Sprengers

    13. March 2017 von Jasper Sprengers

    Thanks for this clear and concise introduction. The sample project worked out of the box, which is not the experience we’ve had so far using Docker for integration testing. That’s good news I guess 🙂


Your email address will not be published.