安卓的JUnit测试...如何期待一个异常 [英] Android JUnit Testing ... How to Expect an Exception

查看:294
本文介绍了安卓的JUnit测试...如何期待一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林试图写入使用内置的Andr​​oid的JUnit测试框架的一些测试。我正在与测试一个问题,我期待一个异常被抛出。在JUnit中,标注的测试方法是:

Im attempting to write some tests using the built in android Junit testing framework. I am running into a problem with a test where I am expecting an exception to be thrown. In JUnit, the annotation for the test method would be:

@Test(预期= ArithmeticException.class)

不过,在Android中,本次测试失败,的ArithmeticException。

However, in android, this test fails with an ArithmeticException.

据我所知,Android的实现仅仅是JUnit 3中的一个子集,并且不甚至允许标注@Test(必须是@SmallTest,@MediumTest,或@LargeTest,没有那些允许预计= .. '参数),但是这似乎是一个相当显著的测试,并且似乎是Android的测试框架将严重缺乏,如果没有这个功能。

I understand that the Android implementation is only a subset of JUnit 3, and doesnt even allow the annotation @Test (must be @SmallTest, @MediumTest, or @LargeTest, and none of those allow for the 'expected=..' parameter), but this seems like a fairly significant test, and seems like the android testing framework would be seriously lacking if it did not have this feature.

注意:我加入了JUnit的jar到项目,并通过添加和注释,以我的测试方法,测试了这一点。这是有道理的我,为什么标注会被完全忽略,因为Android框架(亚军?)是不是找的注解,只是忽略它。基本上我只是在寻找正确的方式的框架内做到这一点。

Note : I tested this by adding the JUnit jar to the project and by adding and the annotations to my test methods. It makes sense to me why the annotations would be completely ignored because the android framework (runner?) is not looking for that annotation and just ignores it. Basically I'm just looking for the 'right' way to do this within the framework.

推荐答案

本标准的JUnit 3成语这种测试是:

The standard junit 3 idiom for this sort of test was:

public void testThatMethodThrowsException()
{
  try
  {
    doSomethingThatShouldThrow();
    Assert.fail("Should have thrown Arithmetic exception");
  }
  catch(ArithmeticException e)
  {
    //success
  }
}

这篇关于安卓的JUnit测试...如何期待一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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