Class SingletonDockerResourceWrapper<T extends DockerResource>

java.lang.Object
org.junit.rules.ExternalResource
no.mnemonic.commons.junit.docker.SingletonDockerResourceWrapper<T>
Type Parameters:
T - wrapped DockerResource type
All Implemented Interfaces:
org.junit.rules.TestRule

public class SingletonDockerResourceWrapper<T extends DockerResource> extends org.junit.rules.ExternalResource
DockerResource is created with intention to be used as a ClassRule. Since ClassRule is executed once per test class, having multiple test classes within single test execution can cause high resource consumption (starting and stopping Docker container can have great impact on system resources). This wrapper makes sure that single instance of Docker container, defined in wrapped resource, is started just once.

WARNING: This resource does not call after() after completion of tests. Underlying resource should register JVM shutdown hook (default in DockerResource), which is used for shutting down of container.

After all tests are finished, either successfully, with an exception or by user cancellation, the wrapped resource will cleanup stale container.

Initialize SingletonDockerResourceWrapper in the following way as ClassRule:

 @ClassRule
  public static SingletonDockerResourceWrapper<DockerResource> docker = SingletonDockerResourceWrapper.builder()
  .setDockerResource(DockerResource.builder()
     .setImageName("busybox")
     .setReachabilityTimeout(30)
     .addApplicationPort(8080)
     .build())
   .build();
 

Created SingletonDockerResourceWrapper exposes getDockerResource method for retrieving underlying DockerResource which can be used in test.