Search code examples
javajunit5testcontainers

Create custom testcontainer modules?


I am using JUnit5 + Testcontainers.

I see there are Testcontainer modules such as these: https://github.com/testcontainers/testcontainers-java/tree/main/modules

I would like to build my own Testcontainers custom module.

Is there a way for developers to build their own?


Solution

  • Yes, you can extend the GenericContainer class. By overriding methods like containerIsStarted or adding your own methods, you can customize the container's behavior.

    Have a look at how these modules are implemented, e.g., the MongoDB one. It overrides containerIsStarted to initialize a replica set:

    @Override
    protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) {
        if (reused && isReplicationSetAlreadyInitialized()) {
            log.debug("Replica set already initialized.");
        } else {
            initReplicaSet();
        }
    }