docker-junit-rule

null

License

License

Categories

Categories

Docker Container Virtualization Tools JUnit Unit Testing
GroupId

GroupId

com.arakelian
ArtifactId

ArtifactId

docker-junit-rule
Last Version

Last Version

4.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

docker-junit-rule
null
Project URL

Project URL

https://github.com/arakelian/docker-junit-rule
Source Code Management

Source Code Management

https://github.com/arakelian/docker-junit-rule.git

Download docker-junit-rule

How to add to project

<!-- https://jarcasting.com/artifacts/com.arakelian/docker-junit-rule/ -->
<dependency>
    <groupId>com.arakelian</groupId>
    <artifactId>docker-junit-rule</artifactId>
    <version>4.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.arakelian/docker-junit-rule/
implementation 'com.arakelian:docker-junit-rule:4.1.0'
// https://jarcasting.com/artifacts/com.arakelian/docker-junit-rule/
implementation ("com.arakelian:docker-junit-rule:4.1.0")
'com.arakelian:docker-junit-rule:jar:4.1.0'
<dependency org="com.arakelian" name="docker-junit-rule" rev="4.1.0">
  <artifact name="docker-junit-rule" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.arakelian', module='docker-junit-rule', version='4.1.0')
)
libraryDependencies += "com.arakelian" % "docker-junit-rule" % "4.1.0"
[com.arakelian/docker-junit-rule "4.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.immutables : value-annotations jar 2.8.8
junit : junit jar 4.13
org.slf4j : slf4j-api jar 1.7.30

test (7)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-api jar 2.13.3
org.apache.logging.log4j : log4j-core jar 2.13.3
org.apache.logging.log4j : log4j-slf4j-impl jar 2.13.3
org.slf4j : jcl-over-slf4j jar 1.7.30
org.slf4j : jul-to-slf4j jar 1.7.30
com.arakelian : retry jar 3.8.0
com.rabbitmq : amqp-client jar 5.8.0

Project Modules

There are no modules declared in this project.

docker-junit-rule

A junit rule to run docker containers.

Features

  • DockerRule will automatically start and stop your Docker container.
  • DockerRule will share your container across multiple JUnit tests; it will not start and stop your container for each test, allowing your test suites to run much faster.

Requirements

  • Compatible with Java 8+

Usage

Starting a Docker container as part of your unit test is as simple as including a ClassRule that looks like this:

@ClassRule
public static RabbitMqDockerRule rabbitmq = new RabbitMqDockerRule();

To configure your own rule, you'll extend from DockerRule:

public class RabbitDockerRule extends DockerRule {
    public RabbitDockerRule() {
        super(ImmutableDockerConfig.builder() //
                .name("docker-test") //
                .image("rabbitmq:management") //
                .ports("5672") //
                .addStartedListener(container -> {
                    container.waitForPort("5672/tcp");
                    container.waitForLog("Server startup complete");
                }).build());
    }
}

A couple of things to note:

  • Your custom ClassRule will extend from DockerRule
  • Your constructor will provide DockerRule with an immutable configuration that describes the Docker container that needs to be created.
  • The configuration you provide includes a user-defined callback for determining when the container has started. A few helper methods are provided to help you do this.

Customizing Docker Container

DockerRule provides two callback for customizing the Docker container, addHostConfigurer and addContainerConfigurer. In the example below we'll use these callback to configure an Elasticsearch container.

public class ElasticDockerRule extends DockerRule {
    public ElasticDockerRule() {
        super(ImmutableDockerConfig.builder() //
                .name(name) //
                .image(image) //
                .ports("9200") //
                .addHostConfigurer(HostConfigurers.noUlimits()) //
                .addContainerConfigurer(builder -> {
                    builder.env(
                            "http.host=0.0.0.0", //
                            "transport.host=127.0.0.1", //
                            "xpack.security.enabled=false", //
                            "xpack.monitoring.enabled=false", //
                            "xpack.graph.enabled=false", //
                            "xpack.watcher.enabled=false", //
                            "ES_JAVA_OPTS=-Xms512m -Xmx512m");
                }) //
                .build());
    }
}

Notice that we used addHostConfigurer to remove ulimits and addContainerConfigurer to set environment variables.

Installation

The library is available on Maven Central.

Maven

Add the following to your pom.xml:

<repositories>
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

...

<dependency>
    <groupId>com.arakelian</groupId>
    <artifactId>docker-junit-rule</artifactId>
    <version>4.1.0</version>
    <scope>test</scope>
</dependency>

Gradle

Add the following to your build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  testCompile 'com.arakelian:docker-junit-rule:4.1.0'
}

Licence

Apache Version 2.0

Versions

Version
4.1.0
4.0.7
4.0.5
4.0.0
3.5.0
3.4.0
3.3.0
3.2.0
3.1.0
3.0.0
2.3.0
2.2.2
2.2.1
2.2.0
2.1.4
2.1.1
2.1.0
2.0.2
2.0.0
1.7.3
1.7.0
1.6.6
1.6.0
1.5.1
1.5.0
1.2.0
1.1.0