Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android - Mockito cannot mock this class: class android.content.Context, android.location.Location and other interfaces #2066

Open
gurappa opened this issue Oct 14, 2020 · 3 comments

Comments

@gurappa
Copy link

gurappa commented Oct 14, 2020

Had discussion in mailing list, I am asked to create an issue here.
https://groups.google.com/g/mockito/c/yUtrG93BSx4

Issue:
We had Mockito version 2.18.3. Now I upgraded to 3.5.13.
Also Robolectric version is upgraded from 3.8 to 4.4.

So half of 700 tests started failing on different reasons. One of them is as below that mockito can't create Context class.

Other classes it could not mock now, for ex, are many interfaces of our codebase, classes, android.app.Activity, android.location.Location.

Stacktrace:
[net.abc.def.MyTest > FirstTest FAILED
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class android.content.Context.

If you're not sure why you're getting this error, please report to the mailing list.

Java : 1.8
JVM vendor name : Private Build
JVM vendor version : 25.265-b01
JVM name : OpenJDK 64-Bit Server VM
JVM version : 1.8.0_265-8u265-b01-0ubuntu2~16.04-b01
JVM info : mixed mode
OS name : Linux
OS version : 4.4.0-57-generic

You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.

Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class android.content.Context]
at net.abc.def.MyTest.onSetUp(FirstTest.java:51)

Caused by:
org.mockito.exceptions.base.MockitoException: Could not modify all classes [class android.content.Context]
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:153)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
    ... 1 more

    Caused by:
    java.lang.IllegalStateException: 
    Byte Buddy could not instrument all classes within the mock's type hierarchy

    This problem should never occur for javac-compiled classes. This problem has been observed for classes that are:
     - Compiled by older versions of scalac
     - Classes that are part of the Android distribution
        at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:265)
        at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.java:212)
        at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:46)
        at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:43)
        at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:153)
        at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
        at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
        at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
        at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:36)
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMockType(InlineByteBuddyMockMaker.java:379)
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.doCreateMock(InlineByteBuddyMockMaker.java:339)
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.java:318)
        at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:52)
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
        at org.mockito.Mockito.mock(Mockito.java:1949)
        at org.mockito.Mockito.mock(Mockito.java:1860)
        ... 1 more

        Caused by:
        java.lang.UnsupportedClassVersionError: android/os/Bundle has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
            at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
            at java.lang.Class.getDeclaredMethods(Class.java:1975)
            at net.bytebuddy.description.method.MethodList$ForLoadedMethods.<init>(MethodList.java:109)
            at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:8715)
            at net.bytebuddy.dynamic.scaffold.InstrumentedType$Factory$Default$1.represent(InstrumentedType.java:416)
            at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:782)
            at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:757)
            at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.java:364)
            at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
            at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
            at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
            at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
            at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:261)
            ... 16 more ] The mockito message in the stacktrace have useful information, but it didn't help

Versions:
Java : 1.8
JVM vendor name : Private Build
JVM vendor version : 25.265-b01
JVM name : OpenJDK 64-Bit Server VM
JVM version : 1.8.0_265-8u265-b01-0ubuntu2~16.04-b01
JVM info : mixed mode
OS name : Linux
OS version : 4.4.0-57-generic

Question in Stackoverflow:
https://stackoverflow.com/questions/64355256/android-mockito-cannot-mock-this-class-class-android-content-context

My build.gradle has these mockito deps:
testImplementation ‘org.mockito:mockito-inline:3.5.13
androidTestImplementation org.mockito:mockito-core:3.5.13
androidTestImplementation org.mockito:mockito-android:3.5.13

@gurappa gurappa changed the title Android - Mockito cannot mock this class: class android.content.Context Android - Mockito cannot mock this class: class android.content.Context, android.location.Location and other interfaces Oct 14, 2020
@gurappa
Copy link
Author

gurappa commented Oct 15, 2020

Just to rule out if it is due to incompatibility with java version, I tried with 2.* mockito version. It failed but with different error. I can post error in case if required.

Our releases are blocked as bitrise qc builds and distribution tasks are failing. Any quick help/hints will be highly appreciated.

@varunajaygupta
Copy link

@gurappa - Were you able to find any solution for this ?

@fmccown
Copy link

fmccown commented Nov 22, 2021

Upgrading to org.mockito:mockito-core:3.6.28 fixed the problem for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants