/junit-rule-docker

A JUnit rule that manages a Docker container lifecycle around test statements.

Primary LanguageJavaApache License 2.0Apache-2.0

CircleCI Download

Provides a JUnit rule that starts a container prior to test execution, enables access to the exposed ports of the container, and removes the container after test execution.

This artifact is available at JCenter, so it may be included by adding this dependency:

<dependency>
    <groupId>me.itzg.testing</groupId>
    <artifactId>junit-rule-docker</artifactId>
    <version>1.2</version>
    <scope>test</scope>
</dependency>

and the jcenter repository, if you haven't already:

<repository>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <id>bintrary</id>
    <name>bintray</name>
    <url>http://jcenter.bintray.com</url>
</repository>

The following example shows how to use the rule to start an Elasticsearch instance, waiting for Elasticsearch to start, and lookup access to the container's port 9200:

@Rule
public DockerRule dockerRule = new DockerRule("itzg/elasticsearch:5")
                                   .waitForLog("started");

@Test
public void testAccess() throws Exception {

    InetSocketAddress accessToPort = dockerRule.getAccessToPort(9200);
    
    ...