Workaround to use Docker Compose Rule with Junit5
Switch branches/tags
Nothing to show
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/test Add code Oct 11, 2017
.gitignore Add .gitignore Oct 11, 2017
.travis.yml Setup Travis CI (#2) May 22, 2018
LICENSE Initial commit Oct 11, 2017 Add Travis CI build status badge May 22, 2018
pom.xml Upgrade to JUnit platform 1.2.0 (#3) May 24, 2018

Docker Compose Rule With Junit5

Build Status

Workaround to use Docker Compose Rule with Junit5.

Why ?

Docker Compose Rule only works on Junit4 for now.

An issue has been opened to be able to use it with Junit5.

How to run ?

mvn clean install

How does it work ?


version: '3'
    image: 'postgres:10'
      - POSTGRES_PASSWORD=ThisIsMySuperPassword
      - 5432
      test: ["CMD", "psql", "-U", "postgres", "-w", "-c", "SELECT version();"]
      interval: 1s
      timeout: 5s
      retries: 12
    image: 'nginx:1.13'
      - 80


The concept of extension allows us to "extend" the behavior of a test, like we used to do it with Junit4's Rule mechanism.

import com.palantir.docker.compose.DockerComposeRule;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;

import static com.palantir.docker.compose.connection.waiting.HealthChecks.toHaveAllPortsOpen;

public class DockerComposeExtension implements BeforeAllCallback, AfterAllCallback, ParameterResolver {

    private DockerComposeRule docker;

    public DockerComposeExtension() {
        docker = DockerComposeRule.builder()
                .waitingForService("postgres", toHaveAllPortsOpen())
                .waitingForService("nginx", toHaveAllPortsOpen())

    public void beforeAll(ExtensionContext extensionContext) throws Exception {

    public void afterAll(ExtensionContext extensionContext) throws Exception {

    public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return parameterContext.getParameter().getType().equals(DockerComposeRule.class);

    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return docker;



And finally, a example of how to use it in a test.

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.DockerPort;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import static com.mashape.unirest.http.Unirest.get;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SimpleTest {

    public void shouldConnectToDatabase(DockerComposeRule docker) throws SQLException, ClassNotFoundException {
        // Given
        // We need to import the driver

        DockerPort container = docker.containers().container("postgres").port(5432);
        String url = "jdbc:postgresql://" + container.getIp() + ":" + container.getExternalPort() + "/postgres";
        Connection connection = DriverManager.getConnection(url, "postgres", "ThisIsMySuperPassword");

        // When
        ResultSet resultSet = connection.prepareStatement("SELECT 1").executeQuery();

        // Then

    public void shouldReceiveNginxWelcome(DockerComposeRule docker) throws UnirestException {
        // Given
        DockerPort container = docker.containers().container("nginx").port(80);
        String url = "http://" + container.getIp() + ":" + container.getExternalPort();

        // When
        HttpResponse<String> response = get(url).asString();

        // Then
        assertTrue(response.getBody().contains("Welcome to nginx!"));