解决方法
最新版本的Android开发人员工具包包括对可模拟Android库的支持,该库与普通的Android支持库相同,但在任何地方都删除了最终修饰符.这将允许您模拟View和Context等类,而不必担心最终的限制.
请参阅此处的文档
> 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 asapp
andlib
,now share it. Delete$rootDir/build
to regenerate it.