Class DockerResource.Builder<T extends DockerResource.Builder<?>>

java.lang.Object
no.mnemonic.commons.junit.docker.DockerResource.Builder<T>
Direct Known Subclasses:
CassandraDockerResource.Builder, ElasticSearchDockerResource.Builder, MariaDBDockerResource.Builder
Enclosing class:
DockerResource

public static class DockerResource.Builder<T extends DockerResource.Builder<?>> extends Object
Builder to create a DockerResource.

Subclasses of DockerResource can also define own builders extending this builder in order to be able to configure the same properties. The configurable properties are exposed as protected fields which can be passed to the constructor of a subclass. This constructor in turn should pass them to the constructor}. See CassandraDockerResource.Builder as an example.

  • Field Details

    • imageName

      protected String imageName
    • applicationPorts

      protected Set<Integer> applicationPorts
    • exposedPortsRange

      protected String exposedPortsRange
    • reachabilityTimeout

      protected int reachabilityTimeout
    • skipReachabilityCheck

      protected boolean skipReachabilityCheck
    • skipPullDockerImage

      protected boolean skipPullDockerImage
    • dockerClientResolver

      protected Supplier<org.mandas.docker.client.DockerClient> dockerClientResolver
    • environmentVariables

      protected Map<String,String> environmentVariables
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • build

      public DockerResource build()
      Build a configured DockerResource.
      Returns:
      Configured DockerResource
    • setImageName

      public T setImageName(String imageName)
      Set image name of container to use. The image must be available in Docker, it is not automatically pulled!
      Parameters:
      imageName - Image name
      Returns:
      Builder
    • setApplicationPorts

      public T setApplicationPorts(Set<Integer> applicationPorts)
      Set application ports which will be used inside the container and exposed outside of the container by mapping to ports inside the range specified with setExposedPortsRange(String) or random ports.

      Also see DockerResource.getExposedHostPort(int) for more information.

      Parameters:
      applicationPorts - Set of application ports
      Returns:
      Builder
    • addApplicationPort

      public T addApplicationPort(int applicationPort)
      Add a single application port which will be used inside the container and exposed outside of the container by mapping to a port inside the range specified with setExposedPortsRange(String) or a random port.

      Also see DockerResource.getExposedHostPort(int) for more information.

      Parameters:
      applicationPort - Single application port
      Returns:
      Builder
    • setExposedPortsRange

      public T setExposedPortsRange(String exposedPortsRange)
      Set port range which will be used for exposing ports inside the container to the outside of the container.
      Parameters:
      exposedPortsRange - String in format "firstPort-lastPort" which is used for setting a range of ports
      Returns:
      Builder
    • setReachabilityTimeout

      public T setReachabilityTimeout(int reachabilityTimeout)
      Set timeout in seconds until test for container reachability stops. Defaults to 30 seconds if not set.

      Also see DockerResource.isContainerReachable() for more information.

      Parameters:
      reachabilityTimeout - Timeout in seconds
      Returns:
      Builder
    • skipReachabilityCheck

      public T skipReachabilityCheck()
      Configure DockerResource to skip test for container reachability. Useful if application code implements similar functionality.
      Returns:
      Builder
    • setSkipPullDockerImage

      public T setSkipPullDockerImage(boolean skipPullDockerImage)
      Skip pulling the image if set to true. Default is to pull the image before running
      Parameters:
      skipPullDockerImage - whether to pull the image
      Returns:
      Builder
    • setDockerClientResolver

      public T setDockerClientResolver(Supplier<org.mandas.docker.client.DockerClient> dockerClientResolver)
      Override the default behaviour of how a DockerClient will be created by providing a custom resolver function. Should be used with care, but useful for providing a mock during unit testing, for instance.
      Parameters:
      dockerClientResolver - Customer DockerClient resolver function
      Returns:
      Builder
    • setEnvironmentVariables

      public T setEnvironmentVariables(MapUtils.Pair<String,String>... variables)
      Set multiple environment variables for the container.
      Parameters:
      variables - Array of key-value pairs
      Returns:
      Builder
    • addEnvironmentVariable

      public T addEnvironmentVariable(String key, String value)
      Add an additional environment variable for the container.
      Parameters:
      key - Variable name
      value - Variable value
      Returns:
      Builder