How to use mockableAndroidJar?

2019-06-19 08:21发布

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条回答
Lonely孤独者°
2楼-- · 2019-06-19 09:23

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.

查看更多
登录 后发表回答