You need to use something called TestCoroutineDispatcher
during local unit tests & the best way to use it creating a Rule
.
You can read about this in detail here: https://developer.android.com/codelabs/advanced-android-kotlin-training-testing-survey#3
I would recommend you go through this whole codelab. It will be really helpful.
Update for version 1.6.1
:
Based on this migration guide: https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/MIGRATION.md
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1") {
// https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android
exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
}
Then create a rule like this in your test directory, Notice the StandardTestDispatcher
change:
@ExperimentalCoroutinesApi
class MainCoroutineRule(private val dispatcher: TestDispatcher = StandardTestDispatcher()) :
TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
}
Use it like this, Notice the usage of runTest
& advanceUntilIdle
:
@OptIn(ExperimentalCoroutinesApi::class)
class ListScreenViewModelTest {
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
private lateinit var viewModel: ListScreenViewModel
@Before
fun setUp() {
viewModel = ListScreenViewModel()
}
@Test
fun `add an item to the list of items`() = runTest {
val numberOfItems = viewModel.testList.value.size
viewModel.addItem()
advanceUntilIdle()
assert(viewModel.testList.value.size == numberOfItems + 1)
}
}
Original Answer:
For the solution
Add this dependency:
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2") {
// https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-debug#debug-agent-and-android
exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-debug"
}
Then Create a rule like this in your test directory:
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@ExperimentalCoroutinesApi
class MainCoroutineRule(val dispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) :
TestWatcher(),
TestCoroutineScope by TestCoroutineScope(dispatcher) {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(dispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
cleanupTestCoroutines()
Dispatchers.resetMain()
}
}
Use it like this:
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Before
import org.junit.Rule
import org.junit.Test
class ListScreenViewModelTest {
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
private lateinit var viewModel: ListScreenViewModel
@Before
fun setup(){
viewModel = ListScreenViewModel()
}
@Test
fun `add an item to the list of items`(){
val numberOfItems = viewModel.testList.value.size
viewModel.addItem()
assert(viewModel.testList.value.size == numberOfItems+1)
}
}