Mockito与Kotlin:AbstractMethodError

时间:2017-08-26 09:05:44

标签: android unit-testing mockito

我正在尝试将google的test lab代码示例与kotlin一起使用。但是在将我想要模拟的界面转换为kotlin后,我得到的是AbstractMethodError,我无法解决。

的build.gradle:

testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile "junit:junit:4.12"
testCompile "org.mockito:mockito-core:2.8.47"

testCompile "org.powermock:powermock-module-junit4:1.6.2"
testCompile "org.powermock:powermock-api-mockito:1.6.2"

NotesPresenterTest.kt

 package com.example.android.testing.notes.notes
 import org.mockito.Matchers.any

/**
 * Unit tests for the implementation of [NotesPresenter]
 */
class NotesPresenterTest {

    @Mock
    private val mNotesView: NotesContract.View? = null

    @Before
    fun setupNotesPresenter() {
        MockitoAnnotations.initMocks(this)

        // Get a reference to the class under test
        mNotesPresenter = NotesPresenter(mNotesRepository!!, mNotesView!!)
    }

    @Test
    fun clickOnFab_ShowsAddsNoteUi() {
        //        // When adding a new note
        mNotesPresenter!!.addNewNote()
        //
        //        // Then add note UI is shown
        verify<NotesContract.View>(mNotesView).showAddNote()
    }

    @Test
    fun clickOnNote_ShowsDetailUi() {
        // Given a stubbed note
        val requestedNote = Note("Details Requested", "For this note")

        // When open note details is requested
        mNotesPresenter!!.openNoteDetails(requestedNote)

        // Then note detail UI is shown
        verify<NotesContract.View>(mNotesView).showNoteDetailUi(any())
    }

    companion object {

        private val NOTES = Lists.newArrayList(Note("Title1", "Description1"),
                Note("Title2", "Description2"))

        private val EMPTY_NOTES = ArrayList<Note>(0)
    }
}

NotesContract.java

public interface NotesContract {

interface View {

    void setProgressIndicator(boolean active);

    void showNotes(List<Note> notes);

    void showAddNote();

    void showNoteDetailUi(String noteId);
}

interface UserActionsListener {

    void loadNotes(boolean forceUpdate);

    void addNewNote();

    void openNoteDetails(@NonNull Note requestedNote);
}

}

这里,在将NotesContract转换为kotlin之后,我得到了下一个错误:

  

java.lang.IllegalStateException:any()不能为null   com.example.android.testing.notes.notes.NotesPresenterTest.clickOnNote_ShowsDetailUi(NotesPresenterTest.kt:98)

所以我添加了Mockito-Kotlin解决它。

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

哪个解决了之前的问题但运行了另一个错误:

  

java.lang.AbstractMethodError:   org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava /郎/类;)Lorg /的Mockito /插件/ MockMaker $ TypeMockability;

     

在   org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)     在   org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)     在   org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)     在   org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)     在org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)at   org.mockito.Mockito.mock(Mockito.java:1729)at   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)     在   org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)     在   org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:38)     在   org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:62)     在   org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)     在   org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)     在   org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:69)     在   com.example.android.testing.notes.notes.NotesPresenterTest.setupNotesPresenter(NotesPresenterTest.kt:58)

有任何想法如何解决问题?

1 个答案:

答案 0 :(得分:0)

看起来Mockito和PowerMock无法很好地协同工作。所以依赖应该是:

testCompile "org.mockito:mockito-core:2.8.47"
testCompile ("com.nhaarman:mockito-kotlin:0.9.0", {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
})

或者,如果您想将PowerMock与JUnit 4.0-4.3一起使用:

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

testCompile "org.powermock:powermock-module-junit4-legacy:$rootProject.ext.powerMockito"
testCompile "org.powermock:powermock-api-mockito2:$rootProject.ext.powerMockito"