模拟AccountManager通过Dagger提供时的Java.lang.reflect.InvocationTargetException。怎么修?

时间:2017-05-18 04:42:58

标签: android unit-testing mockito accountmanager

在我的匕首中,我想提供一个模拟客户经理

@Provides
@Singleton
AccountManager provideAccountManager(Context context) {
    return Mockito.mock(AccountManager.class);
}

我在运行Mockito.mock(AccountManager.class)时遇到以下崩溃,如下所示

    java.lang.ExceptionInInitializerError
    at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(ClassImposterizer.java:85)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:62)
    at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:56)
    at org.mockito.internal.creation.CglibMockMaker.createMock(CglibMockMaker.java:23)
    at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
    at org.mockito.Mockito.mock(Mockito.java:1243)
    at org.mockito.Mockito.mock(Mockito.java:1120)
    at com.mypackage.app.test.TestAndroidServicesModule.provideAccountManager(TestAndroidServicesModule.java:118)
    at com.mypackage.app.test.TestAndroidServicesModule_ProvideAccountManagerFactory.get(TestAndroidServicesModule_ProvideAccountManagerFactory.java:31)
    at com.mypackage.app.test.TestAndroidServicesModule_ProvideAccountManagerFactory.get(TestAndroidServicesModule_ProvideAccountManagerFactory.java:10)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at au.com.mypackage.utils.AccountUtil_Factory.get(AccountUtil_Factory.java:71)
    at au.com.mypackage.utils.AccountUtil_Factory.get(AccountUtil_Factory.java:13)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at au.com.mypackage.utils.ShortcutUtil_Factory.get(ShortcutUtil_Factory.java:41)
    at au.com.mypackage.utils.ShortcutUtil_Factory.get(ShortcutUtil_Factory.java:9)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at au.com.mypackage.app.ui.main.MainActivity_MembersInjector.injectMembers(MainActivity_MembersInjector.java:76)
    at au.com.mypackage.app.ui.main.MainActivity_MembersInjector.injectMembers(MainActivity_MembersInjector.java:13)
    at com.mypackage.app.test.DaggerResiTestComponent.inject(DaggerResiTestComponent.java:1814)
    at au.com.mypackage.app.ui.main.MainActivity.onCreate(MainActivity.java:113)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:532)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
    at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
    at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
    at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
    at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
    ... 35 more
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
    at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
    ... 40 more
    Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
    at java.lang.ClassLoader.defineClass(ClassLoader.java:594)
    ... 43 more

    Test running failed: Instrumentation run failed due to 'Process crashed.'

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

根据David Rawson的评论,我改变了我的依赖性

androidTestCompile 'org.mockito:mockito-all:1.9.5'

androidTestCompile 'org.mockito:mockito-android:2.7.22'

解决了这个问题!