不能初始化MockMaker



这是我的测试类:

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'

我已经创建了src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker,mock-maker-inline在里面。

测试类失败,因为

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.)

如何解决这个问题?SO的答案都没有帮助。

这行得通:

dependencies {
testImplementation 'org.mockito:mockito-core:3.8.0'
androidTestImplementation 'org.mockito:mockito-android:3.8.0'
}

有两个包:mockito-core用于测试,而mockito-android用于android测试。

ref java.lang.IllegalStateException: Could not initialize plugin: MockMaker

我遇到了同样的问题,我已经通过使用testimimplementation解决了这个问题而不是androidtestimimplementation中的build。gradle (app-level):

testImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'

我最近遇到了同样的问题,明确地在Mockito核心旁边添加了以下依赖项,它工作了

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.18</version>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>3.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.12.18</version>
<scope>test</scope>
</dependency>

相关内容

  • 没有找到相关文章

最新更新