惩戒库/框架,在Android的效果最好? [英] Mocking library/framework that works best in Android?

查看:117
本文介绍了惩戒库/框架,在Android的效果最好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是第三方库(Twitter4j)开发Android应用程序。我希望能够在模拟的JUnit测试和功能测试的对象(对象也由我创建)。

I'm developing Android application using third party libraries (Twitter4j). I want to be able mock those objects (also objects created by me) in JUnit and functional tests.

不要使用一些嘲讽库,你有什么好的经验,你可以建议他们?

Do you have any good experiences using some mocking libraries and you can recommend them?

推荐答案

(更新:的Mockito增加了支持Android为1.9.5版本,EasyMock的已通过分解出这些位增加支持Android的3.2版本产生code运行时,使他们可插入,例如使用dexmaker而不是CGLIB。)

除了通过DixonD(这是一个相当年轻的,未经证实的库提 Android的模拟 ),目前尚无解决办法。您可以立即忘掉基于CGLIB(的Mockito ,普通的 EasyMock的),因为CGLIB依赖字节code一代,不会对Dalvik的工作(这也依赖于在Java Beans包,也不是安卓的一部分)。

Except for android-mock mentioned by DixonD (which is a fairly young, unproven library), there currently is no solution. You can immediately forget anything based on CGLib (Mockito, plain EasyMock), since CGLib relies on byte code generation and won't work on Dalvik (it also relies on the Java Beans package, which is also not part of Android).

有关它的价值,你可以用很少的mock类与Android未来(如 MockContext ),但他们没有核实的行为,他们只是存根。其默认行为是在每一个方法抛出一个运行时错误,所以你必须继承它们并覆盖要模拟的方法。

For what it's worth, you could use the very few mock classes coming with Android (like MockContext), but they don't verify behavior, they're just stubs. Their default behavior is to throw a runtime error in every method, so you have to subclass them and override the methods you want to mock.

不过,您仍然可以使用非仪器测试,即嘲讽图书馆在JVM上执行的标准单元测试。您可以使用 PowerMock 以模拟架构的方法,它有嘲讽的静态方法和构造,使得嘲讽支持作为强大如在Ruby中(只是更痛苦的使用)。

However, you can still use mocking libraries in non-instrumentation tests, i.e. in your standard unit tests executed on the JVM. You can use PowerMock to mock framework methods, it has support for mocking static methods and constructors, making the mocking as powerful as e.g. in Ruby (just more painful to use).

我们使用JUnit 4 + PowerMock +的Mockito和模拟出类像语境与文本实用程序从我们继承每一个正常的JUnit测试的基类。对于仪器仪表的测试中,我们创建自定义的模拟类,并决定利用工厂,实现(模拟或不)实例在运行。

We use JUnit 4 + PowerMock + Mockito and mock out classes like Context and TextUtils in a base class from which we inherit every normal JUnit test. For instrumentation tests, we create custom mock classes and decide using a factory which implementation (mock or not) to instantiate at runtime.

这篇关于惩戒库/框架,在Android的效果最好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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