I am trying to mock this function below by using Mockk
library.
fun launchOn(block: suspend CoroutineScope.() -> Unit) {
viewModelScope.launch(Dispatchers.IO) { block() }
}
I tried to mock following below
private val viewModel: PersonalInfoViewModel = mockk(relaxed = true)
every { viewModel.launchOn(any()) } answers {callOriginal()}
but I am getting ClassCastException
. I also tried to use coEvery
and hint
but nothing changed. I could not find much information about this situation in documentations.
My questions are, Where did i make mistake? What is the proper way to mock this function?
Thanks
Exception:
class java.lang.Object cannot be cast to class kotlinx.coroutines.CoroutineScope (java.lang.Object is in module java.base of loader 'bootstrap'; kotlinx.coroutines.CoroutineScope is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @17490d9e)
java.lang.ClassCastException: class java.lang.Object cannot be cast to class kotlinx.coroutines.CoroutineScope (java.lang.Object is in module java.base of loader 'bootstrap'; kotlinx.coroutines.CoroutineScope is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @17490d9e)
at androidx.lifecycle.ViewModelKt.getViewModelScope(ViewModel.kt:37)
at com.biletdukkani.common.base.BaseViewModel.launchOn(BaseViewModel.kt:106)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:83)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:20)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:28)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:227)
at io.mockk.MockKAnswerScope.callOriginal(API.kt:2184)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions$2.invoke(PersonalInfoViewModelTest.kt:57)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions$2.invoke(PersonalInfoViewModelTest.kt:57)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2092)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2069)
at io.mockk.FunctionAnswer.answer(Answers.kt:19)
at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:54)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at com.biletdukkani.common.base.BaseViewModel.launchOn(BaseViewModel.kt:107)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModel.getProfileInfo(PersonalInfoViewModel.kt:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:83)
at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:20)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:28)
at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:227)
at io.mockk.MockKAnswerScope.callOriginal(API.kt:2184)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions$6.invoke(PersonalInfoViewModelTest.kt:59)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest$initViewModelFunctions$6.invoke(PersonalInfoViewModelTest.kt:59)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2092)
at io.mockk.MockKStubScope$answers$1.invoke(API.kt:2069)
at io.mockk.FunctionAnswer.answer(Answers.kt:19)
at io.mockk.impl.stub.AnswerAnsweringOpportunity.answer(AnswerAnsweringOpportunity.kt:14)
at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:54)
at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:263)
at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:25)
at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:20)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModel.getProfileInfo(PersonalInfoViewModel.kt:32)
at com.biletdukkani.b2c.fragments.personalInfo.PersonalInfoViewModelTest.getProfileInfo(PersonalInfoViewModelTest.kt:72)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
CodePudding user response:
I figured it out.
I split my function in 2 parts. Then, I mocked executeProfileInfoGet
part. Now, I don't need to mock launchOn
function anymore.
functions:
fun getProfileInfo() = launchOn {
executeProfileInfoGet()
}
suspend fun executeProfileInfoGet(){
executeFlow(profileInfoRepository.getProfileInfo()) {
when (it) {
is DataState.Success -> {
initCaches(it.response)
checkPassportFields(it.response)
profileInfoLiveData.postValue(it.response)
}
is DataState.Error -> {
showError(it.error.message)
}
else -> Unit
}
}
}
mocked in test class:
coEvery { viewModel.executeProfileInfoGet() } coAnswers { callOriginal() }