Home > Software engineering >  Create custom testcontainer modules?
Create custom testcontainer modules?

Time:02-03

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 TestContainer custom module.

Is there a way for developers to build their own?

CodePudding user response:

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();
    }
}
  • Related