androidunit-testingkotlinkotlin-coroutinesmockk

Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize


Hey I am getting this kind of weird issue. I don't understand why this is causing in my unit test. Can someone please guide me what is missing in my side.

Exception in thread "Test worker" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.missing(MainDispatchers.kt:118)
at kotlinx.coroutines.internal.MissingMainCoroutineDispatcher.isDispatchNeeded(MainDispatchers.kt:96)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:319)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
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 io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14) .....

this is my unit test file. I don't understand why this is causing the issue.

UnitTest.kt

class XYZViewModelTest {

    @get:Rule
    val koinTestRule = KoinTestRule.create {
        modules(utilsModule)
    }

    @get:Rule
    val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()

    private lateinit var mockViewModel: XYZViewModel
    ..../// more declartions
    @Before
    fun setUp() {
        MockKAnnotations.init(this, relaxed = true)
        loadKoinModules(module {
            listOf(
                factory { mockSessionHelper }
            )
        })
        mockViewModel = spyk(XYZViewModel())
    }
     ./// function
}

Solution

  • When unit testing, you have to replace Dispatchers.Main with a different dispatcher than it has by default, because the default implementation of Dispatchers.Main doesn't exist when not running a full application. To do this, you need to have the kotlinx-coroutines-test test dependency if you don't already:

    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0'
    

    You can create a coroutine testing rule to set this up before and after each test, so you can simplify handling this in each of your test classes.

    Put this in its own file:

    @ExperimentalCoroutinesApi
    class MainDispatcherRule(val dispatcher: TestDispatcher = StandardTestDispatcher()): TestWatcher() {
    
        override fun starting(description: Description?) = Dispatchers.setMain(dispatcher)
    
        override fun finished(description: Description?) = Dispatchers.resetMain()
    
    }
    

    And then add the rule to your test class:

    @ExperimentalCoroutinesApi
    @get:Rule
    val mainDispatcherRule = MainDispatcherRule()
    

    Your tests that use coroutines should use and return the runTest function so they should look like this:

    @ExperimentalCoroutinesApi
    @Test
    fun fooTest() = runTest {
        //...
    }
    

    Sorry if I've missed anything. There are a lot of changes in the latest kotlinx-coroutines-test 1.6.0 version and I'm not up to speed on it yet.