如何使用mockableAndroidJar? [英] How to use mockableAndroidJar?

查看:276
本文介绍了如何使用mockableAndroidJar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在使用模仿或其他方法为我们的项目创建一个单元测试框架.我发现有一个任务应用程序:mockableAndroidJar,可以构建一个模拟的AndroidJar jar文件.这是为了什么?如何使用它?我尚未找到有关它的介绍.

Now I am creating a unit test framework of our project using mockito or something else. I find there is a task app:mockableAndroidJar which can build a mockableAndroidJar jar file. What's it for? How to use it? I have not found introduction about it.

推荐答案

包括Mockito在内的多个模拟框架通过重写模拟或监视类上的方法来工作.但是,Android库使用许多final类和方法,它们更适合于嵌入式设备的编译(因为它们跳过了虚拟方法的查找),但不便于模拟.

Several mock frameworks, including Mockito, work by overriding methods on the mocked or spied classes. However, the Android library uses a number of final classes and methods, which are more appropriate for compiling for embedded devices (because they skip virtual method lookups) but are not friendly to mock.

Android开发者工具包的最新版本包括对可模拟Android库的支持,该库与普通Android支持库相同,但在所有地方都删除了final修饰符.这样您就可以模拟ViewContext之类的类,而不必担心final的局限性.

Recent versions of the Android developer kit include support for a mockable Android library, which is identical to the normal Android support library but removes the final modifier everywhere. This will allow you to mock classes like View and Context without worrying about final limitations.

在此处查看文档:

单元测试在开发计算机上的本地JVM上运行.我们的gradle插件将编译src/test/java中找到的源代码,并使用常规的Gradle测试机制执行它.在运行时,将针对已删除所有最终修饰符的android.jar修改版执行测试.这使您可以使用流行的模拟库,例如Mockito.

Unit tests run on a local JVM on your development machine. Our gradle plugin will compile source code found in src/test/java and execute it using the usual Gradle testing mechanisms. At runtime, tests will be executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.

  • 用于Gradle的Android插件发行说明

    添加了可共享的android.jar共享,该插件仅生成一次并用于单元测试.现在,多个模块(例如applib)可以共享它.删除$rootDir/build重新生成.

    Added sharing of the mockable android.jar, which the plugin generates only once and uses for unit testing. Multiple modules, such as app and lib, now share it. Delete $rootDir/build to regenerate it.

  • 这篇关于如何使用mockableAndroidJar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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