12

im investigating testing of coroutines in my Android application and following this code lab Advanced Android in Kotlin 05.3: Testing Coroutines and Jetpack integrations

this codelab contains the following code snippet

@ExperimentalCoroutinesApi
val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()

@ExperimentalCoroutinesApi
@Before
fun setupDispatcher() {
    Dispatchers.setMain(testDispatcher)
}

@ExperimentalCoroutinesApi
@After
fun tearDownDispatcher() {
    Dispatchers.resetMain()
    testDispatcher.cleanupTestCoroutines()
}

however TestCoroutineDispatcher is marked as deprecated with this comment:-

/**
 * [CoroutineDispatcher] that performs both immediate and lazy execution of coroutines in tests
 * and uses a [TestCoroutineScheduler] to control its virtual clock.
 *
 * By default, [TestCoroutineDispatcher] is immediate. That means any tasks scheduled to be run without delay are
 * immediately executed. If they were scheduled with a delay, the virtual clock-time must be advanced via one of the
 * methods on the dispatcher's [scheduler].
 *
 * When switched to lazy execution using [pauseDispatcher] any coroutines started via [launch] or [async] will
 * not execute until a call to [DelayController.runCurrent] or the virtual clock-time has been advanced via one of the
 * methods on [DelayController].
 *
 * @see DelayController
 */
@Deprecated("The execution order of `TestCoroutineDispatcher` can be confusing, and the mechanism of " +
    "pausing is typically misunderstood. Please use `StandardTestDispatcher` or `UnconfinedTestDispatcher` instead.",
    level = DeprecationLevel.WARNING)
// Since 1.6.0, ERROR in 1.7.0 and removed as experimental in 1.8.0
public class TestCoroutineDispatcher(public override val scheduler: TestCoroutineScheduler = TestCoroutineScheduler()):
    TestDispatcher(), Delay, SchedulerAsDelayController
{...}

It is not clear how I should use the suggested alternatives to TestCoroutineDispatcher() are StandardTestDispatcher and UnconfinedTestDispatcher straight replacements for TestCoroutineDispatcher()?

what am I missing?

Hector
  • 4,016
  • 21
  • 112
  • 211
  • 1
    I use `UnconfinedTestDispatcher` as `testDispatcher` and `runTest()` in unit-test. See also https://craigrussell.io/2022/01/19/comparing-standardtestdispatcher-and-unconfinedtestdispatcher, https://medium.com/@ralf.stuckert/testing-coroutines-update-1-6-0-701d53546683. – CoolMind Apr 01 '22 at 21:16

2 Answers2

2

Seems it is possible to use more elegant solution like runTest() since 1.6.0

Taken from this SO answer

See the documentation for details on how to use the module.

Joe Dow
  • 583
  • 1
  • 3
  • 12
1

Ide suggests StandardTestDispatcher(), however, its not behaving like TestCoroutineDispatcher() as StandardTestDispatcher() won't enter launch{} or async{} immediately. Therefore, its better to use UnconfinedTestDispatcher(). I suggest you to check the quick documentation in android studio to get the idea.

Xarybdis
  • 157
  • 6