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

问题描述:

在我的匕首,我想提供一个模拟的客户经理当通过Dagger提供模拟AccountManager时,Java.lang.reflect.InvocationTargetException。怎么修?

@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

哪个版本的Mockito?请使用最新的mockito-android使用ByteBuddy,而不是cglib,即[This one](http://search.maven.org/#artifactdetails%7Corg.mockito%7Cmockito-android%7C2.7.22%7Cjar) –

+0

谢谢。这有助于! – Elye

基于大卫罗森的评论,我改变从

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

我的相关性要

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

即解决这个问题!

+1

他他。很高兴帮助! –