事實證明,JUnit希望@BeforeClass
和@AfterClass
是靜態的,這與JerseyTest的configure
方法重寫不相符。有沒有一種已知的方式來配置Jersey應用程序,同時仍然能夠訪問JUnit的實用程序方法?如何在JerseyTest套件中使用@BeforeClass和@AfterClass
public class MyControllerTest extends JerseyTest {
@BeforeClass
public static void setup() throws Exception {
target("myRoute").request().post(Entity.json("{}"));
}
@Override
protected Application configure() {
return new AppConfiguration();
}
}
因此beforeClass
必須是靜態的,target
不能使用,因爲它的實例方法自然的調用。在嘗試使用構造函數時,結果發現configure
在constructor
之後運行,這可以防止設置請求被執行,並因此自然失敗。
任何建議是超過讚賞,謝謝!
那麼,爲什麼你想要'setup()'方法是靜態的呢?難道你不能讓它成爲會員,而是使用'@ Before'來代替? – hfhc2
Didi你試過'@ Before'而不是'@ BeforeClass'? – Dimitri
它意味着沉重且可能耗時的操作,我不想在每次單獨測試中運行。 @Dimitri – Ivo