Home » DevOps » A Docker Maven Plugin for Integration Testing

About Bilgin Ibryam

Bilgin is a software craftsman based in London, integration architect at Red Hat, Apache Camel and Apache OFBiz committer. He is an open source fanatic, passionate about distributed systems, messaging, enterprise integration patterns, and application integration. He is also the author of Camel Design Patterns and Instant Apache Camel Message Routing books.

A Docker Maven Plugin for Integration Testing

What is Docker?

Docker is the buzzword that is taking the DevOps world. If you don’t know yet what is Docker, be warned, you will find yourself using it one way or another very soon. The rest of this post assumes some basic understanding of Docker, but if you are not familiar with it now, I’m sure you will come back later and read this.

Docker is ideal for integration testing, complex demos of distributed systems or even running production systems. It is an open source software container. You can imagine it as a very lightweight and ultra fast virtual machine.

An example

Inspired by the “Integration testing with Maven and Docker” article and using Docker Java API I’ve created a simple Docker Maven Plugin that can manage Docker containers. Given a Docker image, the plugin will create a container from it and start it as part of maven build process and stop and remove the container when the build process is finished. If the image is not available locally it will pull it down from the public Docker registry before creating a container from it.

The following integration test in Apache Camel is ignored because it requires a running Redis instance:

package org.apache.camel.component.redis;

import org.apache.camel.impl.JndiRegistry;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisProducerIntegrationTest extends RedisTestSupport {
    private static final JedisConnectionFactory CONNECTION_FACTORY = new JedisConnectionFactory();

    static {

    protected JndiRegistry createRegistry() throws Exception {
        JndiRegistry registry = super.createRegistry();
        redisTemplate = new RedisTemplate();

        registry.bind("redisTemplate", redisTemplate);
        return registry;

    public void shouldSetAString() throws Exception {
                RedisConstants.COMMAND, "SET",
                RedisConstants.KEY, "key1",
                RedisConstants.VALUE, "value");

        assertEquals("value", redisTemplate.opsForValue().get("key1"));

    public void shouldGetAString() throws Exception {
        redisTemplate.opsForValue().set("key2", "value");
        Object result = sendHeaders(RedisConstants.KEY, "key2", RedisConstants.COMMAND, "GET");

        assertEquals("value", result);

To make it pass, we can use docker-maven-plugin with a Redis image and 6379 port accessible for the test:

                            "PortBindings": {
                                "6379/tcp": [
                                        "HostIp": "",
                                        "HostPort": "6379"

The plugin will start a Docker container at compile phase that has a running Redis instance and shut it down at post-integration-test phase.

This is a very simple example, but the plugin can support more advanced scenarios with multiple images configured differently and started/stopped at different phases. Enjoy.

Reference: A Docker Maven Plugin for Integration Testing from our JCG partner Bilgin Ibryam at the OFBIZian blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments