CoroutineTestRule

class CoroutineTestRule(testDispatcher: TestDispatcher = UnconfinedTestDispatcher()) : TestWatcher

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
}
}

Constructors

Link copied to clipboard
constructor(testDispatcher: TestDispatcher = UnconfinedTestDispatcher())

Functions

Link copied to clipboard
open override fun apply(p0: Statement, p1: Description): Statement