Android中如何测试assert抛出异常
在 Android 中是否有更优雅的方法来执行断言抛出异常?
public void testGetNonExistingKey() {
try {
alarm.getValue("NotExistingValue");
fail( );
} catch (ElementNotFoundException e) {
}
}
这样的事不行吗?!
@Test(expected=ElementNotFoundException .class)
谢谢,马克
is there a more elegant way to do an assert throws exception in Android then this?
public void testGetNonExistingKey() {
try {
alarm.getValue("NotExistingValue");
fail( );
} catch (ElementNotFoundException e) {
}
}
Something like this does not work?!
@Test(expected=ElementNotFoundException .class)
Thanks, Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您使用的是 junit4 测试运行程序吗?如果您正在运行 junit3 测试运行程序,则 @Test 注释将不起作用。检查您正在使用的版本。
其次,检查代码中异常的推荐方法是使用规则(在 junit 4.7 中引入)。
您可以继续使用@Test(expected=IOException.class),但上面的好处是,如果在调用exception.expect之前抛出异常,那么测试将失败。
Are you using a junit4 test runner? The @Test annotation won't work if you're running a junit3 test runner. Check the version that you're using.
Secondly, the recommended way to check for exceptions in your code is to use a Rule (introduced in junit 4.7).
You can continue to use the @Test(expected=IOException.class), but the above has the advantage that if an exception is thrown before the exception.expect is called, then the test will fail.
我做了一些与hopia的答案非常相似的事情,但做了一些改进。我让它返回异常对象,以便您可以检查其消息或任何其他属性,并且我声明了一个 Testable 接口来替换 Runnable 因为 Runnable > 不允许您的测试代码抛出已检查的异常。
这是调用它的示例。
I did something very similar to hopia's answer with a couple of improvements. I made it return the exception object so that you can check its message or any other properties, and I declared a
Testable
interface to replaceRunnable
becauseRunnable
doesn't let your code under test throw checked exceptions.Here's an example of calling it.
如果您使用 Kotlin,则可以利用 具体化类型 以避免将 Exception 子类作为参数传递:
If you're using Kotlin, you can take advantage of reified types to avoid passing the Exception subclass as an argument:
我就是这样做的。我创建了一个名为assertThrowsException 的静态方法,该方法接受预期异常类和包含被测试代码的Runnable 作为参数。
这是在测试类(扩展 AndroidTestCase 或其衍生物之一)中使用特殊断言的示例代码:
是的,有很多工作,但它比将 junit4 移植到 android 更好。
This is how I do it. I create a static method called assertThrowsException that takes in as arguments an expected exception class and a Runnable which contains the code under test.
This is the sample code to use the special assert in your test class (that extends AndroidTestCase or one of its derivatives):
Yes, there's a lot of work, but it's better than porting junit4 to android.
对于 junit3,以下内容可能会有所帮助。
With junit3 the following might help.