2013-04-12 91 views
2

我試圖編寫一個自定義的Runner,它只是按隨機順序執行測試。亞軍:JUnit自定義亞軍不工作?

public class TestClassRunnerForParameters extends BlockJUnit4ClassRunner { 
    public TestClassRunnerForParameters(Class<?> type) throws Exception { 
     super(type); 
    } 

    protected java.util.List<org.junit.runners.model.FrameworkMethod> computeTestMethods() { 
     java.util.List<org.junit.runners.model.FrameworkMethod> methods = super 
       .computeTestMethods(); 
     Collections.shuffle(methods); 
     return methods; 
    } 
} 

現在這工作正常,如果它不是參數化測試。使用參數測試可以做到這一點嗎?實現參數化接口?

回答

0

只需添加一個構造函數(如建議):

public TestClassRunnerForParameters(Class testClass) { 
    ... 
} 

而且有它委託給你的構造。在這種情況下,您的構造函數應該是public,因爲JUnit/Surefire正在使用反射。

+1

我做了同樣的錯誤:java.lang.Exception的:自定義亞軍類TestClassRunnerForParameters應該有簽名TestClassRunnerForParameters(類識別TestClass) \t在org.junit.runners.model.InitializationError公共構造<初始化 –

+0

@HansEn編輯您的問題顯示您所做的更改。 –

+0

你甚至閱讀錯誤信息說什麼? – carlspring

1

我想說的錯誤是相當自我descripting:

Custom runner class TestClassRunnerForParameters should have a public constructor with signature TestClassRunnerForParameters(Class testClass)

類中是否有該簽名沒有構造函數。它唯一的構造函數具有參數Class<?> type,List<Object[]> parameterListint i。你應該刪除後面兩個參數。另外,該構造函數不是public;你應該在它前面加上public

此外,如果您嘗試構建參數化測試,則可能會將其集成到org.junit.runners.Parameterized跑步者中,因爲它確實如此。這是一個很好的tutorial

+0

好了,但仍然有一個錯誤: –

+0

java.lang.Exception:測試類應該只有一個公共零參數構造函數 –

+0

這聽起來像是你的測試類中的一個問題,而不是你的跑步者類,不是嗎? – mthmulders