【问题标题】:Android - Mockito cannot mock this class: class android.content.ContextAndroid - Mockito 无法模拟此类:class android.content.Context
【发布时间】:2020-10-14 14:15:45
【问题描述】:

我们有 Mockito 版本 2.18.3。现在我升级到 3.5.13。
Robolectric 版本也从 3.8 升级到 4.4。

因此,在 700 次测试中,有一半因各种原因开始失败。其中之一是 mockito 无法创建 Context 类。

它现在无法模拟的其他类,例如,我们代码库的许多接口、类、android.app.Activity、android.location.Location。

任何帮助将不胜感激。谢谢!

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

【问题讨论】:

  • If you're not sure why you're getting this error, please report to the mailing list.
  • 已经完成了,谢谢。

标签: android unit-testing exception mockito


【解决方案1】:

我们有 2 岁的 gradle 版本,即 3.3.1。所以,当我尝试将 gradle 版本直接升级到最新的 4.x 时,运行单元测试时出现了一些问题。所以不得不提高 mockito 并且最终导致了这个问题。

变得凌乱,不知道如何解决。

只是改变了方法 - 开始逐个提升 gradle 版本,而不是直接从这么旧的版本跳到最新版本。现在一切顺利。

因此,如果您处于相同的情况..逐个提高版本,而不是长时间跳跃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多