"No such network" 尝试通过网络重用 Testcontainers 规则时出错 class

"No such network" error when trying to re-use Testcontainers rules with network from abstract class

假设我有一个名为 AbstractTestWithNetwork 的摘要 class:

@RunWith(JUnit4.class)
public abstract class AbstractTestWithNetwork {

    @ClassRule
    public static Network network = Network.newNetwork();

    @ClassRule
    public static GenericContainer etcd = new GenericContainer<>("alpine:3.6")
            .withNetwork(network);
}

我想简单地重新使用它,通过扩展它在多个 classes 中拥有相同的容器:

public class FirstTestClass extends AbstractTestWithNetwork {
    @Test
    public void emptyMethod() throws Exception {
        System.out.println("An empty test method");
    }
}

还有一个SecondTestClass内容相同

我可以 运行 每个 class 与 IDE 分开,它们都会通过。但是当我 运行 gradle test 或从 IDE 中选择带有测试 classes 的整个包时,只有第一个测试 class 通过。第二次我得到:

org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by:
org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
Caused by:
org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by:
java.lang.reflect.UndeclaredThrowableException
Caused by:
java.lang.reflect.InvocationTargetException
Caused by:com.github.dockerjava.api.exception.NotFoundException: {"message":"No such network: a48cf082ab42a55c843e9963c3938f44dd93cceae09e1724d4fefd5b45f235f1"}

我检查了实现并发现了 a bug in Networks' implementation

在 TestContainers <= 1.4.2 中,网络实例不可重用,即不能与 @ClassRule 一起使用。

但我有一个好消息要告诉您 - 在修复之前有一个简单的解决方法:只需从 "network" 字段中删除 @ClassRule。