Package no.mnemonic.commons.junit.docker
Class SingletonDockerResourceWrapper<T extends DockerResource>
java.lang.Object
org.junit.rules.ExternalResource
no.mnemonic.commons.junit.docker.SingletonDockerResourceWrapper<T>
- Type Parameters:
T
- wrappedDockerResource
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.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder to create aSingletonDockerResourceWrapper
. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
before()
static <T extends DockerResource>
SingletonDockerResourceWrapper.Builder<T>builder()
Create builder forSingletonDockerResourceWrapper
.Return wrappedDockerResource
.Methods inherited from class org.junit.rules.ExternalResource
after, apply
-
Method Details
-
before
- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
Throwable
-
getDockerResource
Return wrappedDockerResource
.- Returns:
- Wrapped
DockerResource
-
builder
Create builder forSingletonDockerResourceWrapper
.- Returns:
- Builder object
-