这是我的测试类:
class RocketListVMTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var sut: RocketListVM
private var activeOnlyToggle = false
private val repo: Repo = mock()
@Before
fun setUp() {
sut = RocketListVM(repo)
activeOnlyToggle = false
}
@Test
fun toggleActiveOnlyWithTrueCallsRepository() {
sut.toggleActiveOnly(true)
verify(repo).getActiveOnlyLocalRockets()
}
}
具有以下依赖关系:
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'
我已经创建了包含mock-maker-inline
的src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker
。
测试类失败是因为
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@dd6cba3
Caused by: org.mockito.exceptions.base.MockitoInitializationException:
Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
如何解决这个问题?所有的答案都不起作用。
发布于 2021-03-03 16:48:50
这是可行的:
dependencies {
testImplementation 'org.mockito:mockito-core:3.8.0'
androidTestImplementation 'org.mockito:mockito-android:3.8.0'
}
有两个包: mockito-core用于测试,mockito-android用于android测试。
参考java.lang.IllegalStateException: Could not initialize plugin: MockMaker
发布于 2021-12-02 04:33:37
我也遇到过同样的问题,我在build.gradle (应用级)中使用testImplementation而不是androidTestImplementation解决了这个问题:
testImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
https://stackoverflow.com/questions/66343356
复制相似问题