I am using Spring Boot and running tests in Testcontainers.
Sometimes (when developing) I would like to run tests not against Testcontainers, but against already running containers.
Is there a way to disable Testcontainers depending on Spring profiles, environment variables, etc.?
Right now I am commenting the container injection code and regularly check them in like that.
As recommended by Sergei here https://github.com/testcontainers/testcontainers-java/issues/2833#event-3405411419
this is the solution:
public class FixedHostPortGenericDisableableContainer<T extends FixedHostPortGenericDisableableContainer<T>> extends FixedHostPortGenericContainer<T> {
private boolean isActive;
public FixedHostPortGenericDisableableContainer(@NotNull String dockerImageName) {
super(dockerImageName);
}
@Override
public void start() {
if (isActive) {
super.start();
}
}
public FixedHostPortGenericDisableableContainer isActive(boolean isActive) {
this.isActive = isActive;
return this;
}
}
Usage
// set this environment variable to true to disable test containers
public static final String ENV_DISABLE_TEST_CONTAIENRS = "DISABLE_TEST_CONTAIENRS";
@Container
private static GenericContainer dynamoDb =
new FixedHostPortGenericDisableableContainer("amazon/dynamodb-local:1.11.477")
.isActive(StringUtils.isBlank(System.getenv(ENV_DISABLE_TEST_CONTAIENRS)))
.withFixedExposedPort(8001, 8000)
.withStartupAttempts(100);