2

assert(false);导致测试失败的可接受方式是什么?

此外,如果在尝试访问类的私有方法时抛出 NoSuchMethodExceptionSecurityException,我是否应该导致 TestNG 测试失败,或者我应该只打印异常的堆栈跟踪?

@Test
public void getLevelTest() {
    try {
        menu.getClass().getDeclaredMethod("getLevel",int.class,String.class);
    } catch (NoSuchMethodException | SecurityException e) {
        assert(false);
    }
}
4

3 回答 3

3

在 TestNG 框架中处理预期异常的另一种方法是:

@Test(expectedExceptions = {NoSuchMethodException.class, SecurityException.class})

如果带有此注释的测试遇到任何异常,则将其视为通过。

于 2013-06-28T18:29:18.897 回答
2

在这种情况下,使用assert(false)意味着您丢失了有关引发什么异常的信息。TestNG 不知道异常与测试失败有什么关系。

您不需要断言任何东西或捕获任何东西:

@Test
public void getLevelTest() throws Exception {
        menu.getClass().getDeclaredMethod("getLevel",int.class,String.class);
}

测试框架将捕获异常并在测试结果中打印堆栈跟踪。

于 2013-06-28T18:28:02.100 回答
2

使用assertTrue()and assertFalse(),另请参阅此主题的旧答案

于 2013-06-28T18:30:35.910 回答