Android Mockito与Kotlin:AbstractMethodError
我试图让谷歌的代码示例与kotlin一起工作。但在将我想要模拟的接口转换为kotlin之后,我得到了一个Android Mockito与Kotlin:AbstractMethodError,android,unit-testing,mockito,Android,Unit Testing,Mockito,我试图让谷歌的代码示例与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:power
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"
注释resentest.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)
}
}
它解决了以前的问题,但运行时出现另一个错误:
java.lang.AbstractMethodError:
org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable(Ljava/lang/Class;)Lorg/mockito/plugins/mockmaker$TypeMockability
在
org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
在
org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
在
org.mockito.internal.creation.mockSettingSiml.validatedSettings(mockSettingSiml.java:186)
在
org.mockito.internal.creation.mockSettingSiml.confirm(mockSettingSiml.java:180)
位于org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
org.mockito.mockito.mock(mockito.java:1729)位于
org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33)
在
org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16)
在
org.mockito.internal.configuration.IndendentAnnotationEngine.createMockFor(IndendentAnnotationEngine.java:38)
在
org.mockito.internal.configuration.IndendentAnnotationEngine.process(IndendentAnnotationEngine.java:62)
在
org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:57)
在
org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:41)
在
initMocks(MockitoAnnotations.java:69)
在
com.example.android.testing.notes.NotesPresenterTest.setupNotesPresenter(NotesPresenterTest.kt:58)
有什么办法解决这个问题吗?看起来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'
})
或者,如果您想与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"
你会想用PowerMock吗=D
testCompile "org.mockito:mockito-core:2.8.47"
testCompile ("com.nhaarman:mockito-kotlin:0.9.0", {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
})
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"