Skip to content

Example needed: Singleton container #1138

Closed

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.

Activity

  1. chungngoops commented on Jan 16, 2019

    @chungngoops
    Contributor

    I would like to take this issue.

  2. kiview commented on Jan 24, 2019

    @kiview
    Member

    Hey @chungngoops, this is great!
    You already started with it, or have any questions?

  3. chungngoops commented on Jan 24, 2019

    @chungngoops
    Contributor

    @kiview : I already started with it, I will have my PR ASAP!

  4. SushmaReddyLoka commented on Dec 29, 2020

    @SushmaReddyLoka

    Does this example support running tests ,
    examples/singleton-container/src/test/java/com/example/BarConcreteTestClass.java and
    examples/singleton-container/src/test/java/com/example/FooConcreteTestClass.java
    in parallel ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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