Newest Mockito Android fails on Android P
See original GitHub issueThe latest releases of Mockito Android appear to break compatibility with Android P. Specifically in instrumentation tests, I encounter an error when attempting to mock an Android framework class. Here’s the mocking code I’m using:
@Override
protected ConnectivityManager provideConnectivityManager_impl(Context context) {
NetworkInfo info = mock(NetworkInfo.class);
when(info.isConnectedOrConnecting()).thenReturn(true);
ConnectivityManager manager = provide(ConnectivityManager.class);
when(manager.getActiveNetworkInfo()).thenReturn(info);
return manager;
}
Here is the error I encounter:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class android.net.NetworkInfo.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
IMPORTANT INFORMATION FOR ANDROID USERS:
The regular Byte Buddy mock makers cannot generate code on an Android VM!
To resolve this, please use the 'mockito-android' dependency for your application:
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22mockito-android%22%20g%3A%22org.mockito%22
Java : 0.9
JVM vendor name : The Android Project
JVM vendor version : 2.1.0
JVM name : Dalvik
JVM version : 0.9
JVM info : null
OS name : Linux
OS version : 4.4.124+
Underlying exception : java.lang.IllegalArgumentException: On Android P, a class injection can only be applied to BaseDexClassLoader: net.bytebuddy.dynamic.loading.MultipleParentClassLoader@ecc1346
at com.blueapron.service.mock.InstrumentAppModule.provideConnectivityManager_impl(InstrumentAppModule.java:63)
at com.blueapron.service.dagger.ApplicationModule.provideConnectivityManager(ApplicationModule.java:149)
at com.blueapron.service.dagger.ApplicationModule_ProvideConnectivityManagerFactory.proxyProvideConnectivityManager(ApplicationModule_ProvideConnectivityManagerFactory.java:44)
at com.blueapron.service.dagger.ApplicationModule_ProvideConnectivityManagerFactory.provideInstance(ApplicationModule_ProvideConnectivityManagerFactory.java:33)
at com.blueapron.service.dagger.ApplicationModule_ProvideConnectivityManagerFactory.get(ApplicationModule_ProvideConnectivityManagerFactory.java:28)
at com.blueapron.service.dagger.ApplicationModule_ProvideConnectivityManagerFactory.get(ApplicationModule_ProvideConnectivityManagerFactory.java:10)
The class doesn’t appear to be final or private, making the error message confusing.
I have verified that the same exact test passes with the 2.19.0 libraries, and fails with anything higher, starting at 2.20.1 and continuing through the 2.23.0 libraries. So it doesn’t appear to be a configuration issue with my instrumentation tests. (I initially thought this was caused by incorrectly including mockito-core instead of mockito-android, but since I can produce the failure by only changing the mockito version, this feels unlikely)
Any ideas what has gone wrong here?
I do solemnly swear that:
- [ X ] The mockito message in the stacktrace have useful information, but it didn’t help
- [ X ] The problematic code (if that’s possible) is copied here; Note that some configuration are impossible to mock via Mockito
- [ X ] Provide versions (mockito / jdk / os / any other relevant information)
- [ X ] Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
- [ X ] Read the contributing guide
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:13 (3 by maintainers)
Top GitHub Comments
It looks like this may be fixed in the newest byte-buddy versions. Unpinning my byte buddy and updating to Mockito 2.24.0 (which brings in byte-buddy 1.9.7) seems to fix the problem on my builds!
@plastiv
Check the answer from: https://stackoverflow.com/a/51891596/8034839
Try to use the version 2.18.3