question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newest Mockito Android fails on Android P

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tmtrademarkedcommented, Feb 7, 2019

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!

1reaction
russell-shizhencommented, Nov 23, 2018

@plastiv

Check the answer from: https://stackoverflow.com/a/51891596/8034839

Try to use the version 2.18.3

testImplementation 'org.mockito:mockito-core:2.18.3'
androidTestImplementation 'org.mockito:mockito-android:2.18.3'
Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.IllegalArgumentException: On Android P, a class ...
1 Answer 1 · 1. implementation 'org.mockito:mockito-android:2.18.3' seems to be working fine now, still the same problem if i use 2.21.0. – Arpit....
Read more >
HttpURLConnection - Android Developers
URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection ... If the HTTP response indicates that an error occurred, URLConnection.
Read more >
Building with jack fails due to cryptic AssertionError [37096224]
While trying out the new build tools (24.0.0-rc3) with AS 2.1, I enabled jack in our project and I am getting a weird...
Read more >
Mockito framework site
A landing page for information about Mockito framework, a mocking framework for unit tests written in Java.
Read more >
Full Guide to Testing Android Applications in 2022
Creating a NewActivityViewModel triggers the emission of a new Error UI state in case of error;; If Activity is already saved, the UI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found