【问题标题】:How to run junit 3 with non default constructor如何使用非默认构造函数运行 junit 3
【发布时间】:2013-03-13 21:45:11
【问题描述】:

我有一个代码库,他们将 junit 测试用例定义为:

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

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

如何从 Eclipse 运行这个测试?如何在构造函数中提供名称?

【问题讨论】:

  • new MyTest("testSome")).run();来自某个 Eclipse 主文件?

标签: java unit-testing tdd junit3


【解决方案1】:

如果您正在创建实现,为什么不自己传递超类型构造参数,即

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

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

【讨论】:

  • 我认为您的意思是删除 MyTest 构造函数的参数?
【解决方案2】:

你不能像 JUnit runner 期望 'Test class should have exactly one public zero-argument constructor' 那样直接运行它,所以你必须手动调用它,或者像 @shim cat 显示的那样,或者每个类都这样做

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 ");
}

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多