2017-10-04 58 views
0

假设我有一个抽象类,称为AbstractTestWithNetwork规则:“没有这样的网络”的错误尝试的时候重新使用Testcontainers与网络从抽象类

@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); 
} 

我想简单地重新使用它有通过扩展它在几类相同的容器:

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

还有一个SecondTestClass具有相同的内容。

我可以单独从IDE运行每个类,它们会通过。但是当我运行gradle test或从IDE中选择包含测试类的整个包时,只有第一个测试类通过。对于第二个我得到:

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"} 

回答

2

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

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

但我对你有好消息 - 有一个简单的解决方法,直到它被修复:只是从“网络”字段中删除@ClassRule。