Android - Mockito cannot mock this class: class android.content.Context

1,891 views
Skip to first unread message

Gurappa Chenchugari

unread,
Oct 14, 2020, 11:57:48 AM10/14/20
to mockito
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.
Also gradle version from 3.3.1 to 4.0.0

--------------------------------
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

Tim van der Lippe

unread,
Oct 14, 2020, 11:58:55 AM10/14/20
to mockito
The root cause lists the following:

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

Which version of Java are you using? https://github.com/mockito/mockito/issues/2065 might be helpful.

Op wo 14 okt. 2020 om 16:57 schreef Gurappa Chenchugari <gurappa.c...@easypark.net>:
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/81eea6a1-fe78-4acc-87ff-a575cdfadf54n%40googlegroups.com.

Gurappa Chenchugari

unread,
Oct 14, 2020, 12:46:04 PM10/14/20
to mockito
This is what I have in my Android Studio:

java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

As per https://site.mockito.org/ :
“Mockito 3.x requires Java 8, but otherwise doesn’t introduce any breaking changes compared to the 2.x series.”

so looks like that is not a problem ?

Tim van der Lippe

unread,
Oct 14, 2020, 12:47:28 PM10/14/20
to mockito
Ah bummer. Please file an issue on our issue tracker with a minimal reproduction case and we can take a look.

Op wo 14 okt. 2020 om 17:46 schreef Gurappa Chenchugari <gurappa.c...@easypark.net>:
Reply all
Reply to author
Forward
0 new messages