Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mockableAndroidJar?

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.

like image 948
Victor Choy Avatar asked Jan 18 '16 11:01

Victor Choy


People also ask

How do you mock a jar class?

It is possible to mock a class that it is inside a jar so I don't call the real method. public class Person{ private PersonalExternalJar pej; public void methodA(){ *do some stuff* pej = new PersonalExternalJar(); ArrayList people = pej.

What is Mockito Android?

Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.


1 Answers

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.

like image 169
Jeff Bowman Avatar answered Sep 28 '22 01:09

Jeff Bowman