How can I get Mockito working in androidTest
By default, Mockito uses CGLib or ByteBuddy, both of which generate .class
files. You're running on an Android device or emulator, so .class
files won't help; you need .dex
format instead.
Adjust your dependencies to use DexMaker, which will override Mockito's default and allow mocking in Android environments.
Since version 2.6.+, Mockito added a new artifact that works for Android without the need of any other dependency (i.e. no need to import DexMaker anymore). (Reference)
Just use org.mockito:mockito-android
as a dependency for your instrumented unit tests (androidTest
). You would still use the usual org.mockito:mockito-core
for your local unit tests.
Example:
dependencies {
...
testImplemenation 'org.mockito:mockito-core:2.7.15'
androidTestImplementation 'org.mockito:mockito-android:2.7.15'
...
}