Skip to content

Example needed: Singleton container #1138

@bsideup

Description

@bsideup

There is a pattern which we never documented but it is helpful when you want to have a single container per JVM. We call it "Singleton container".

The idea is trivial and does not depend on any testing framework:

First, define a base class for the tests, let's say AbstractIntegrationTest.
Then, define a container as a static property:

class AbstractIntegrationTest {
    public static final GenericContainer redis = new GenericContainer(...)
                                                             .withExposedPorts(...); 
}

Now we need to start it, but only once. Without relying on the testing framework, we can do it with a static block (JVM will trigger it only once, on class loading):

class AbstractIntegrationTest {
    public static final GenericContainer redis = ...; 

    static {
        redis.start();
    }
}

That's it! Every test class with extends AbstractIntegrationTest will have a container started & running, and the container will be terminated only on JVM shutdown.

An example should have at least 2 different classes with the tests to demonstrate it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions