2013-03-13 93 views
-1

我有一個代碼庫,他們定義的JUnit測試案例:如何運行JUnit 3使用非默認的構造函數

public class MyTest extends BaseTestCase 
{ 
    public MyTest(String name) 
    { 
     super(name); 
    } 

    public void testSome() throws Exception 
    { 
     assertTrue (1 == 1); 
    } 
} 

怎麼辦我從Eclipse運行這個測試?我如何在構造函數中提供名稱?

+0

new MyTest(「testSome」))。run();從一些eclipse主文件? – 2013-03-13 21:49:01

回答

1

您不能直接運行它的JUnit運行預期'Test class should have exactly one public zero-argument constructor'所以你必須手動或@shim貓顯示調用它或做每類

protected void setUp() throws Exception { 
    System.out.println(" local setUp "); 
} 
protected void tearDown() throws Exception { 
    System.out.println(" local tearDown "); 
} 

,但如果你想分享你可每次做到這一點「的TestSuite」

protected void setUp() throws Exception { 
    System.out.println(" Global setUp "); 
} 
protected void tearDown() throws Exception { 
    System.out.println(" Global tearDown "); 
} 
2

如果你正在創建的實施,爲什麼不通過超類型的施工參數,即自己

public class MyTest extends BaseTestCase 
{ 
    public MyTest() 
    { 
     super("My Test"); 
    } 

    public void testSome() throws Exception 
    { 
     assertTrue (1 == 1); 
    } 
} 
+0

我想你打算將參數移除到MyTest構造函數中? – sharakan 2013-03-13 22:22:02

+0

謝謝,我現在編輯它 – GuessBurger 2013-03-13 22:23:21