CoroutineTestRule
A JUnit Test Rule that sets the main coroutine dispatcher to a TestDispatcher for unit testing.
This rule allows you to replace the main dispatcher with a test dispatcher, which can be controlled during tests. It uses an UnconfinedTestDispatcher by default, but a custom TestDispatcher can be provided.
Example usage:
class ExampleTest {
@get:Rule
val coroutineTestRule = CoroutineTestRule()
@Test
fun exampleTest() = runTest {
// Given a scenario that requires coroutines
// When executing coroutine-based code
// Then verify the expected outcome
}
}
Content copied to clipboard