2016-07-01 15 views
1

Ich habe eine instrumentale Espresso-Test, der Mockito verwendet. Die Testklasse wie folgt.java.lang.AbstractMethodError: abstrakte Methode beim Ausführen von Espresso auf Kotlin mit 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"))) 
    } 

} 

Meine TestComponentRule wie unten

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

Und mein Dolch 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) 
    } 
} 

Und meine build.gradle Datei wie unten

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

Wenn ich meine Instrumentation Test auslösen , es ähm ror auf

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

und

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

mit dem Fehler

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)  

Antwort

0

Anscheinend entfernen Sie die unten Mockito-Kotlin Bibliothek von meinem build.gradle lösen das Problem

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

U PDATED

das Problem Berichtet nhaarman, wie pro https://github.com/nhaarman/mockito-kotlin/issues/46