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.
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.
Conclusion. Mockito is used to test final classes in Kotlin. Mockito is a widely used framework for mobile developers to write unit tests.
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 asapp
andlib
, now share it. Delete$rootDir/build
to regenerate it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With