有没有一种方法可以在 1 个 dockercompose(测试容器)实例上执行连续的 n 个集成测试
Is there a way of executing consecutive n number integration-tests over 1 instances of dockercompose (test-containers)
在 SpringBootApp 中使用来自测试容器 api 的 DockerComposeContainer 我遇到以下问题。测试容器中的 DockerComposeContainer 对象必须用 @ClassRule 注释才能自动启动和关闭,由于 @ClassRule 注释,这就需要字段是静态的。如果我执行单个测试,它会成功完成,但是当我执行超过 1 个连续测试时,我在第二次测试中出错
Could not start container
org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container cn6j5atqrtav_janusgraph_1 as it is not running
at org.testcontainers.containers.GenericContainer..
这是因为我为 DockerComposeContainer 使用静态字段。如果我只使用@Rule,那么我无法将 DockerComposeContainer 实例传递给使用该字段的@TestConfiguration 内部静态 class 的 bean。为什么我应该使用内部静态 class,因为我从 SpringBoot 应用程序的上下文中覆盖了一些 bean。这是一些小例子:
@RunWith(SpringRunner.class)
public class IntegrationTestBase {
private static final String DOCKER_COMPOSE_FILE = "src/test/resources/integration/docker-compose-cql-es.yml";
@ClassRule
public static DockerComposeContainer environment =
new DockerComposeContainer(new File(DOCKER_COMPOSE_FILE))
.withExposedService("janusgraph", 8182, Wait.forListeningPort()
.withStartupTimeout(Duration.ofMinutes(1)));
@TestConfiguration
public static class ContextConfig {
@Bean(Constants.GRAPH_TRAVERSAL_SOURCE)
@Scope("prototype")
public GraphTraversalSource graphTraversalSource() {
org.apache.commons.configuration.Configuration remoteConfig = new BaseConfiguration();
remoteConfig.addProperty("gremlin.remote.remoteConnectionClass", "org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection");
remoteConfig.addProperty("gremlin.remote.driver.sourceName", "g");
remoteConfig.addProperty("gremlin.remote.driver.clusterFile", "remote-objects.yaml");
try {
org.apache.commons.configuration.Configuration clusterConfig = new PropertiesConfiguration();
clusterConfig.addProperty("hosts", new String[]{environment.getServiceHost("janusgraph", 8182)}); // I want to use environemnt here without beign static ?
clusterConfig.addProperty("port", environment.getServicePort("janusgraph", 8182));
clusterConfig.addProperty("serializer.className", GryoMessageSerializerV3d0.class.getName());
clusterConfig.addProperty("serializer.config.ioRegistries", JanusGraphIoRegistry.class.getName());
clusterConfig.addProperty("serializer.config.ioRegistries", JanusGraphIoRegistry.class.getName());
final Cluster cluster = Cluster.open(clusterConfig);
Constructor<DriverRemoteConnection> constructor = DriverRemoteConnection.class.getDeclaredConstructor(Cluster.class, boolean.class, String.class);
constructor.setAccessible(true);
DriverRemoteConnection connection = constructor.newInstance(cluster, true, "g");
return JanusGraphFactory.open("inmemory").traversal().withRemote(connection);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
我尝试过的一件事是从 DockerComposeContainer 中删除静态并使用 @Rule 而不是 @ClassRule 并使内部 class 非静态但是我不覆盖当前的 SpringBoot 环境和我需要使用 @Import IntegrationTestBase 到内部 class 到 @AutoWired DockerComposeContainer,这样我就遇到了循环依赖问题。对不起,如果问题太宽泛了。 class 是一个基础 class 将被测试 classes 扩展,因为它里面没有 @Test。 Link 描述了 SpringBoot 测试的配置 https://howtodoinjava.com/spring-boot2/testing/springboot-test-configuration/
The DockerComposeContainer object from test-containers must be annotated with @ClassRule to be started and closed automatically
没有必须,而是应该。
您可以完全删除注释并使用 the Singleton Container pattern,例如。
在 SpringBootApp 中使用来自测试容器 api 的 DockerComposeContainer 我遇到以下问题。测试容器中的 DockerComposeContainer 对象必须用 @ClassRule 注释才能自动启动和关闭,由于 @ClassRule 注释,这就需要字段是静态的。如果我执行单个测试,它会成功完成,但是当我执行超过 1 个连续测试时,我在第二次测试中出错
Could not start container
org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container cn6j5atqrtav_janusgraph_1 as it is not running
at org.testcontainers.containers.GenericContainer..
这是因为我为 DockerComposeContainer 使用静态字段。如果我只使用@Rule,那么我无法将 DockerComposeContainer 实例传递给使用该字段的@TestConfiguration 内部静态 class 的 bean。为什么我应该使用内部静态 class,因为我从 SpringBoot 应用程序的上下文中覆盖了一些 bean。这是一些小例子:
@RunWith(SpringRunner.class)
public class IntegrationTestBase {
private static final String DOCKER_COMPOSE_FILE = "src/test/resources/integration/docker-compose-cql-es.yml";
@ClassRule
public static DockerComposeContainer environment =
new DockerComposeContainer(new File(DOCKER_COMPOSE_FILE))
.withExposedService("janusgraph", 8182, Wait.forListeningPort()
.withStartupTimeout(Duration.ofMinutes(1)));
@TestConfiguration
public static class ContextConfig {
@Bean(Constants.GRAPH_TRAVERSAL_SOURCE)
@Scope("prototype")
public GraphTraversalSource graphTraversalSource() {
org.apache.commons.configuration.Configuration remoteConfig = new BaseConfiguration();
remoteConfig.addProperty("gremlin.remote.remoteConnectionClass", "org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection");
remoteConfig.addProperty("gremlin.remote.driver.sourceName", "g");
remoteConfig.addProperty("gremlin.remote.driver.clusterFile", "remote-objects.yaml");
try {
org.apache.commons.configuration.Configuration clusterConfig = new PropertiesConfiguration();
clusterConfig.addProperty("hosts", new String[]{environment.getServiceHost("janusgraph", 8182)}); // I want to use environemnt here without beign static ?
clusterConfig.addProperty("port", environment.getServicePort("janusgraph", 8182));
clusterConfig.addProperty("serializer.className", GryoMessageSerializerV3d0.class.getName());
clusterConfig.addProperty("serializer.config.ioRegistries", JanusGraphIoRegistry.class.getName());
clusterConfig.addProperty("serializer.config.ioRegistries", JanusGraphIoRegistry.class.getName());
final Cluster cluster = Cluster.open(clusterConfig);
Constructor<DriverRemoteConnection> constructor = DriverRemoteConnection.class.getDeclaredConstructor(Cluster.class, boolean.class, String.class);
constructor.setAccessible(true);
DriverRemoteConnection connection = constructor.newInstance(cluster, true, "g");
return JanusGraphFactory.open("inmemory").traversal().withRemote(connection);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
我尝试过的一件事是从 DockerComposeContainer 中删除静态并使用 @Rule 而不是 @ClassRule 并使内部 class 非静态但是我不覆盖当前的 SpringBoot 环境和我需要使用 @Import IntegrationTestBase 到内部 class 到 @AutoWired DockerComposeContainer,这样我就遇到了循环依赖问题。对不起,如果问题太宽泛了。 class 是一个基础 class 将被测试 classes 扩展,因为它里面没有 @Test。 Link 描述了 SpringBoot 测试的配置 https://howtodoinjava.com/spring-boot2/testing/springboot-test-configuration/
The DockerComposeContainer object from test-containers must be annotated with @ClassRule to be started and closed automatically
没有必须,而是应该。
您可以完全删除注释并使用 the Singleton Container pattern,例如。