java.lang.AbstractMethodError:用Mockito在Kotlin上运行espresso时的抽象方法

我有一个使用mockito的器乐浓缩咖啡测试。 测试类如下。

import android.support.test.InstrumentationRegistry import android.support.test.rule.ActivityTestRule import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.rules.RuleChain import org.junit.rules.TestRule import android.support.test.espresso.Espresso.onView import android.support.test.espresso.assertion.ViewAssertions.matches import android.support.test.espresso.matcher.ViewMatchers.withId import android.support.test.espresso.matcher.ViewMatchers.withText import org.mockito.Mockito.`when` class MainActivityTest { val component = TestComponentRule(InstrumentationRegistry.getTargetContext()) val main = ActivityTestRule(MainActivity::class.java, false, false) // TestComponentRule needs to go first so we make sure the ApplicationTestComponent is set // in the Application before any Activity is launched. @JvmField @Rule var chain: TestRule = RuleChain.outerRule(component).around(main) @Before fun setUp() { } @Test fun simpleTrueTest() { `when`(component.mockInjectedData.status).thenReturn(true) main.launchActivity(null) onView(withId(R.id.txt_myview)).check(matches(withText("True"))) } @Test fun simpleFalseTest() { `when`(component.mockInjectedData.status).thenReturn(false) main.launchActivity(null) onView(withId(R.id.txt_myview)).check(matches(withText("False"))) } } 

我的TestComponentRule如下

 import android.content.Context import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement class TestComponentRule(val context: Context) : TestRule { private var mTestComponent: ApplicationTestComponent? = null val mockInjectedData: InjectedData get() = mTestComponent!!.dataManager() private fun setupDaggerTestComponentInApplication() { val application = MainApplication[context] mTestComponent = DaggerApplicationTestComponent.builder().applicationTestModule(ApplicationTestModule(application)).build() application.component = mTestComponent as ApplicationComponent } override fun apply(base: Statement, description: Description): Statement { return object : Statement() { @Throws(Throwable::class) override fun evaluate() { try { setupDaggerTestComponentInApplication() base.evaluate() } finally { mTestComponent = null } } } } } 

还有我的Dagger TestModule

 import android.app.Application import javax.inject.Singleton import dagger.Module import dagger.Provides import org.mockito.Mockito.mock @Module class ApplicationTestModule(protected val mApplication: Application) { @Provides internal fun provideApplication(): Application { return mApplication } @Provides @Singleton internal fun provideInjectedData(): InjectedData { return mock(InjectedData::class.java) } } 

和我的build.gradle文件如下

 dependencies { final SUPPORT_LIBRARY_VERSION = '23.4.0' final DAGGER_VERSION = '2.2' final DEXMAKER_VERSION = '1.4' final MOCKITO_VERSION = '1.10.19' final ESPRESSO_VERSION = '2.2.1' final JUNIT_VERSION = '4.12' final RUNNER_VERSION = '0.4' compile fileTree(dir: 'libs', include: ['*.jar']) testCompile "junit:junit:$JUNIT_VERSION" compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION" compile "com.google.dagger:dagger:$DAGGER_VERSION" kapt "com.google.dagger:dagger-compiler:$DAGGER_VERSION" kaptAndroidTest "com.google.dagger:dagger-compiler:$DAGGER_VERSION" apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION" androidTestApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION" provided 'org.glassfish:javax.annotation:10.0-b28' compile "org.jetbrains.kotlin:kotlin-stdlib:$KOTLIN_VERSION" androidTestCompile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION" androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") { exclude group: 'com.android.support', module: 'appcompat' exclude group: 'com.android.support', module: 'support-v4' exclude group: 'com.android.support', module: 'recyclerview-v7' } androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION" androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION" androidTestCompile "com.android.support.test:runner:$RUNNER_VERSION" androidTestCompile "com.android.support.test:rules:$RUNNER_VERSION" androidTestCompile "org.mockito:mockito-core:$MOCKITO_VERSION" androidTestCompile "com.crittercism.dexmaker:dexmaker:$DEXMAKER_VERSION" androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:$DEXMAKER_VERSION" androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:$DEXMAKER_VERSION" androidTestCompile ("com.nhaarman:mockito-kotlin:0.4.1") { exclude group: "org.jetbrains.kotlin", module: 'kotlin-stdlib' } } 

当我触发我的仪器测试时,它的错误

  `when`(component.mockInjectedData.status).thenReturn(true) 

  `when`(component.mockInjectedData.status).thenReturn(false) 

与错误

 java.lang.AbstractMethodError: abstract method "org.mockito.plugins.MockMaker$TypeMockability org.mockito.plugins.MockMaker.isTypeMockable(java.lang.Class)" at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:26) at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:167) at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:161) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:54) at org.mockito.Mockito.mock(Mockito.java:1449) at org.mockito.Mockito.mock(Mockito.java:1362) 

显然从我的build.gradle中删除下面的Mockito-Kotlin库解决了这个问题

 androidTestCompile ("com.nhaarman:mockito-kotlin:0.4.1") { exclude group: "org.jetbrains.kotlin", module: 'kotlin-stdlib' } 

更新

向nhaarman报告这个问题,按照https://github.com/nhaarman/mockito-kotlin/issues/46