JUnit测试用例中"fail"的实际用法是什么? [英] What's the actual use of 'fail' in JUnit test case?

查看:1578
本文介绍了JUnit测试用例中"fail"的实际用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JUnit测试用例中,'fail'的实际用法是什么?

What's the actual use of 'fail' in JUnit test case?

推荐答案

在某些情况下,我发现它很有用:

Some cases where I have found it useful:

  • 标记一个未完成的测试,它会失败并警告您,直到您可以完成
  • 确保引发异常:
try{
  // do stuff...
  fail("Exception not thrown");
}catch(Exception e){
  assertTrue(e.hasSomeFlag());
}

注意:

自JUnit4以来,有一种更优雅的方法来测试是否引发了异常: 使用注释@Test(expected=IndexOutOfBoundsException.class)

Since JUnit4, there is a more elegant way to test that an exception is being thrown: Use the annotation @Test(expected=IndexOutOfBoundsException.class)

但是,如果您还想检查异常,则此方法将无效,那么您仍然需要fail().

However, this won't work if you also want to inspect the exception, then you still need fail().

这篇关于JUnit测试用例中"fail"的实际用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆