【问题标题】:How to create a JUnit 4 test suite to pass when expected exceptions are thrown如何创建一个 JUnit 4 测试套件以在引发预期异常时通过
【发布时间】:2011-07-29 00:44:53
【问题描述】:

我正在编写一个 JUnit 4 测试套件,它运行一些测试来检查是否引发了异常。就其本身而言,我的测试看起来有点像这样:

@RunWith(value=BlockJUnit4ClassRunner.class)
public class CreateCommandsExceptionsTest extends TestCase {
    Statistics statistics;
    @Before
    public void setUp(){
        ...
    }
        ...
    @Test (expected=StatsArrayTooShortException.class)
    public void testStatsArrayTooShortException(){
        ...
    }

测试本身运行良好,但是当我尝试将它们放入测试套件时,它们会失败,因为我正在测试的异常被抛出。

我的测试套件如下所示:

@RunWith(value=BlockJUnit4ClassRunner.class)
public class UnitTestSuite {
public static testSuite(){
    TestSuite suite = new TestSuite("Test for unitTests");

    suite.addTestSuite(StatisticsTest.class);
    ...
    return suite;
}
}

如果有人能告诉我应该如何设置我的测试套件,以便在我发现预期的异常时通过测试,我将不胜感激。

【问题讨论】:

    标签: unit-testing exception junit annotations test-suite


    【解决方案1】:

    您在 Junit 4 中使用了错误的套件语法。

    这是正确的方法:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({
            JunitTest1.class,
            JunitTest2.class
    })
    public class JunitTest5 {
    }
    

    【讨论】:

    • 行得通!谢谢!你能为 JUnit 推荐任何好的文档吗?我似乎找不到任何
    • junit.sourceforge.net - junit4 食谱junit.org - junit 的主站点
    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2011-07-22
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多