Repository: mockito/mockito
Issue: Newest Mockito Android fails on Android P
Labels: android

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](http://sscce.org) of the issue
       (same as any question on stackoverflow.com)
 - [ X ] Read the [contributing guide](https://github.com/mockito/mockito/blob/master/.github/CONTRIBUTING.md)




Issue comments:
This is probably broken by https://github.com/mockito/mockito/commit/28fa11092b55595727eb3c40059a5f632f3f29cc Could you revert the Bytebuddy version and see if that fixes it?
Sure thing, @TimvdLippe - I'm not 100% sure on the best way to go about that, though. Any pointers on how to most easily pull down and point to the correct artifacts? I've got it cloned, but am not sure which gradle projects represent the `core` and `android` artifacts.

(I'm assuming that `subprojects/android` is likely `mockito-android - but just trying to include the root project in my app gives a gradle error, making me suspect I'm doing this wrong)
You should be able to pin the version of ByteBuddy to a specific version in your `pom.xml` or Gradle file. No need to clone this repository :smile: 
So this is fixed as of #1525 ?
> So this is fixed as of #1525 ? 

Doesn't look so. `mockito-android 2.23.0` and `bytebuddy 1.9.3` fails with exception. same mockito and `bytebuddy 1.8.10` works for me 
Use 2.23.4
> Use 2.23.4

How should it fix the issue?

```
+--- org.mockito:mockito-android:2.23.4
|    +--- org.mockito:mockito-core:2.23.4
|    |    +--- net.bytebuddy:byte-buddy:1.9.3
|    |    +--- net.bytebuddy:byte-buddy-agent:1.9.3
|    |    \--- org.objenesis:objenesis:2.6
|    \--- net.bytebuddy:byte-buddy-android:1.9.3
|         +--- net.bytebuddy:byte-buddy:1.9.3
|         \--- com.jakewharton.android.repackaged:dalvik-dx:1
|              \--- com.jakewharton.android.repackaged:libcore-dex:2
```
Fails, but 
```
+--- org.mockito:mockito-android:2.23.4
|    +--- org.mockito:mockito-core:2.23.4
|    |    +--- net.bytebuddy:byte-buddy:1.9.3 -> 1.8.10
|    |    +--- net.bytebuddy:byte-buddy-agent:1.9.3 -> 1.8.10
|    |    \--- org.objenesis:objenesis:2.6
|    \--- net.bytebuddy:byte-buddy-android:1.9.3 -> 1.8.10
|         +--- net.bytebuddy:byte-buddy:1.8.10
|         \--- com.jakewharton.android.repackaged:dalvik-dx:1
|              \--- com.jakewharton.android.repackaged:libcore-dex:2
```
works. What am I missing?
@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'
```

Hmm - it doesn't seem to be working for me, even with a forced ByteBuddy. Here's a code snippet from my build.gradle to force this:

```
configurations.all {
    resolutionStrategy {
        force 'net.bytebuddy:byte-buddy:1.8.10'
        force 'net.bytebuddy:byte-buddy-agent:1.8.10'
    }
}
```

And you can see in my dependencies:
```
+--- org.mockito:mockito-android:2.23.4
|    +--- org.mockito:mockito-core:2.23.4
|    |    +--- net.bytebuddy:byte-buddy:1.9.3 -> 1.8.10
|    |    +--- net.bytebuddy:byte-buddy-agent:1.9.3 -> 1.8.10
|    |    \--- org.objenesis:objenesis:2.6
|    \--- net.bytebuddy:byte-buddy-android:1.9.3
|         +--- net.bytebuddy:byte-buddy:1.9.3 -> 1.8.10
|         \--- com.jakewharton.android.repackaged:dalvik-dx:1
|              \--- com.jakewharton.android.repackaged:libcore-dex:2
```

Any further ideas what might be causing this?
@tmtrademarked Try also downgrading `net.bytebuddy:byte-buddy-android:1.8.10`
Well, I feel silly now. That should have been obvious. Thanks! That does in fact fix the problem.
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!
Glad to hear! Closing per above comment.

Pull request review comments:
Version 3.0.1 requires Java 8. If this is an issue, we need to keep using the older version.
I reverted this change.

Patch that resolved the issue:
diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle
index e5f96c5002..6b43944095 100644
--- a/gradle/dependencies.gradle
+++ b/gradle/dependencies.gradle
@@ -4,7 +4,7 @@ ext {
 
 def versions = [:]
 
-versions.bytebuddy = '1.9.0'
+versions.bytebuddy = '1.9.3'
 versions.junitJupiter = '5.1.1'
 
 libraries.junit4 = 'junit:junit:4.12'
@@ -20,9 +20,10 @@ libraries.bytebuddyandroid = "net.bytebuddy:byte-buddy-android:${versions.bytebu
 
 libraries.errorprone = 'com.google.errorprone:error_prone_core:2.3.1'
 
+// objenesis 3.x with full Java 11 support requires Java 8, bump version when Java 7 support is dropped
 libraries.objenesis = 'org.objenesis:objenesis:2.6'
 
-libraries.asm = 'org.ow2.asm:asm:6.2'
+libraries.asm = 'org.ow2.asm:asm:7.0'
 
 def kotlinVersion = '1.2.10'
 def kotlinVersion_1_3 = '1.3.0-rc-57'


Test patch: