前提・実現したいこと
AndroidStudioでAndroid開発をしています。
言語はkotlinを使用しています。
UnitテストでRobolectricとmockito-kotlinを併用したいのですが、
エラーを解決できません。
発生している問題・エラーメッセージ
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null) at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74) at com.sun.proxy.$Proxy31.isTypeMockable(Unknown Source) at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29) at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22) at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238) at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:68) at org.mockito.Mockito.spy(Mockito.java:1979) at org.mockito.kotlin.SpyingKt.spy(Spying.kt:52) at com.gotcha.gotcha2.usecase.authentication.AuthenticationUseCaseImplTest.setUp(AuthenticationUseCaseImplTest.kt:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575) at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:263) at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@1d73f468 at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57) at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44) at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:21) at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:18) at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24) ... 23 more Caused by: org.mockito.exceptions.base.MockitoInitializationException: Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.) Java : 1.8 JVM vendor name : JetBrains s.r.o JVM vendor version : 25.242-b3-6915495 JVM name : OpenJDK 64-Bit Server VM JVM version : 1.8.0_242-release-1644-b3-6915495 JVM info : mixed mode OS name : Mac OS X OS version : 10.16 at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:171) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49) ... 28 more Caused by: java.lang.IllegalStateException: Mockito failed to inject the MockMethodDispatcher class into the bootstrap class loader It seems like your current VM does not support the instrumentation API correctly. at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:145) ... 34 more Caused by: java.lang.ClassNotFoundException: org.mockito.internal.creation.bytebuddy.MockMethodDispatcher at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:163) at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:129) at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$loadClass$0(SandboxClassLoader.java:115) at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53) at org.robolectric.internal.bytecode.SandboxClassLoader.loadClass(SandboxClassLoader.java:115) at java.lang.ClassLoader.loadClass(ClassLoader.java:352) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:137) ... 34 more
該当のソースコード
import android.content.Context import androidx.test.core.app.ApplicationProvider //省略 import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.Unconfined import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.setMain import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.koin.core.context.loadKoinModules import org.koin.core.context.startKoin import org.koin.core.context.stopKoin import org.koin.core.module.Module import org.koin.dsl.module import org.koin.test.AutoCloseKoinTest import org.koin.test.inject import org.mockito.Mockito import org.mockito.junit.MockitoJUnit import org.mockito.junit.MockitoRule import org.mockito.kotlin.spy import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config @RunWith(RobolectricTestRunner::class) @Config(application = TestApp::class) class AuthenticationUseCaseImplTest : AutoCloseKoinTest() { //省略 @ExperimentalCoroutinesApi @Before fun setUp() { //Mainスレッドを起動 Dispatchers.setMain(Unconfined) // startKoin { modules(listOf(repositoryModule, useCaseModule)) } //successMockモジュールの作成 successMockAuthenticationInformationRepositoryImpl = spy(SuccessMockAuthenticationInformationRepositoryImpl()) successMockRepositoryModule = module { single(override = true) { successMockAuthenticationInformationRepositoryImpl } } //省略 } @After fun tearDown() { stopKoin() } @Test fun changeMailAddressAbNormalTest() = runBlockingSilent { //省略 //ここでモックを利用 val abNormalMailAddressResult = authenticationUseCaseImpl.changeMailAddress(inValidMailAddressValue) assertThat(abNormalMailAddressResult).apply { isInstanceOf(Result.Error::class.java) } //ここでcontextを利用したい val context = ApplicationProvider.getApplicationContext<Context>() if (abNormalMailAddressResult is Result.Error) { assertThat(abNormalMailAddressResult.exception.message).apply { isEqualTo(context.applicationContext.getString(R.string.notification_mail_address_label)) } } }
試したこと
kotlin
1@Before 2fun setup(){ 3MockitoAnnotations.initMock(this) 4} 5@After 6fun teardown(){ 7Mockito.validateMockitoUsage() 8}
を追記
2.
kotlin
1@RunWith(RobolectricTestRunner::class) 2@Config(application = TestApp::class) 3class UseCaseTest : 4 AutoCloseKoinTest() { 5class 6@Rule 7@JvmField 8val rule: MockitoRule = MockitoJUnit.rule()
補足情報(FW/ツールのバージョンなど)
mockito-kotlinのみ、もしくはRobolectricのみではテストが成功しています。
ですので、併用することで起きていると思っています。
関連しそうなライブラリのバージョンは以下です
Gradle:app
// Required -- JUnit 4 framework testImplementation 'junit:junit:4.12' // Optional -- Robolectric environment testImplementation 'androidx.test:core:1.0.0' testImplementation 'org.robolectric:robolectric:4.4' testImplementation "org.mockito.kotlin:mockito-kotlin:3.1.0" testImplementation "org.mockito:mockito-inline:2.23.0"
以上よろしくお願いいたします。
あなたの回答
tips
プレビュー