Testcontainers - 不要在实例化期间自动启动容器

Testcontainers - Do not start container automatically during instantiation

假设我的集成测试 (JUnit 5) 中有以下容器:

@Container
private static GenericContainer databaseContainer =
        new GenericContainer("oscarfonts/h2:latest")
                .withExposedPorts(H2_TCP_PORT, H2_WEB_PORT);

这样容器就会自动启动。有没有办法告诉 testcontainers 不要自动启动容器?

是的,@Container 注解仅用于在测试前启动容器并在测试后将其拆除。

另一种方法是实例化 databaseContainer,然后在 start() 上手动实例化它 - Manual Container Lifecycle Control