2017-10-04 59 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。