Skip to content

Instantly share code, notes, and snippets.

@AniketSK
Last active April 14, 2023 18:56
Show Gist options
  • Save AniketSK/0fd48da9ed969eee307f92457115612a to your computer and use it in GitHub Desktop.
Save AniketSK/0fd48da9ed969eee307f92457115612a to your computer and use it in GitHub Desktop.
A test rule to allow testing coroutines that use the main dispatcher. Without this you'd run into "java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used"
......
dependencies {
def coroutinesVersion = "1.3.0-M1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
testImplementation 'org.hamcrest:hamcrest-library:2.1'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.25.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.25.0'
}
package com.aniketkadam.sharevideoshortcut
import org.junit.rules.TestWatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.runner.Description
@ExperimentalCoroutinesApi
class CoroutineTestRule(private val dispatcher = TestCoroutineDispatcher()) : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.MockitoAnnotations
import org.mockito.Mockito.`when` as wh
@RunWith(JUnit4::class)
class ExampleTest {
@ExperimentalCoroutinesApi
@get:Rule
val coroutineTestRule = CoroutineTestRule()
@ExperimentalCoroutinesApi
@Test
fun exampleTest() = runBlockingTest {
val result = suspendingFunction()
assert(result)
}
}
@mirjalal
Copy link

@AniketSK thank you very much! just adding CoroutineTestRule class and following lines to my test class helped me:

@ExperimentalCoroutinesApi
@get:Rule
val coroutineTestRule = CoroutineTestRule()

@Defuera
Copy link

Defuera commented Jan 9, 2020

class CoroutineTestRule(private val dispatcher = TestCoroutineDispatcher()) : TestWatcher() {

to

class CoroutineTestRule(private val dispatcher: CoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment