How to use mockableAndroidJar?

扶醉桌前 提交于 2019-12-12 10:53:16

问题


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.


回答1:


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.

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.

See documentation here:

  • Android Tools Project Site - Unit testing support

    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.

  • Android Plugin for Gradle Release Notes

    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.



来源:https://stackoverflow.com/questions/34853185/how-to-use-mockableandroidjar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!