2016-07-29 59 views
3

事實證明,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不能使用,因爲它的實例方法自然的調用。在嘗試使用構造函數時,結果發現configureconstructor之後運行,這可以防止設置請求被執行,並因此自然失敗。

任何建議是超過讚賞,謝謝!

+0

那麼,爲什麼你想要'setup()'方法是靜態的呢?難道你不能讓它成爲會員,而是使用'@ Before'來代替? – hfhc2

+0

Didi你試過'@ Before'而不是'@ BeforeClass'? – Dimitri

+0

它意味着沉重且可能耗時的操作,我不想在每次單獨測試中運行。 @Dimitri – Ivo

回答

-2

@Before不需要靜態改性劑和每一個測試方法之前將被執行。

+0

它意味着沉重且可能耗時的操作,我不想在每次單獨測試中運行。 – Ivo

1

在幾種情況下,爲避免在這種情況下進行繁重的設置,我們所做的是使用布爾標誌來有條件地運行該設置。

public class MyControllerTest extends JerseyTest { 

    private static myRouteSetupDone = false; 

    @Before 
    public void setup() throws Exception { 
    if (!myRouteSetupDone) { 
     target("myRoute").request().post(Entity.json("{}")); 
     myRouteSetupDone = true; 
    } 
    } 
    @Override 
    protected Application configure() { 
     return new AppConfiguration(); 
    } 
} 
相關問題