我正在使用Spring Boot并在Testcontainers中运行测试。
有时(在开发时)我不希望针对Testcontainer运行测试,而是针对已经运行的容器运行测试。
有没有一种方法可以根据Spring配置文件,环境变量等来禁用Testcontainer?
现在,我正在评论容器注入代码,并定期检查它们。
正如谢尔盖(Sergei)的建议,这里https://github.com/testcontainers/testcontainers-java/issues/2833#event-3405411419
这是解决方案:
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;
}
}
用法
// 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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句