2013-03-13 82 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