Skip to content

[Test Improver] Modernize MainCoroutineRule: replace deprecated TestCoroutineDispatcher/runBlockingTest #3644

@github-actions

Description

@github-actions

🤖 Test Improver — automated AI assistant for test improvements.

MainCoroutineRule.kt uses APIs deprecated since kotlinx-coroutines-test 1.6:

  • TestCoroutineDispatcher → replaced by StandardTestDispatcher / UnconfinedTestDispatcher
  • runBlockingTest → replaced by runTest
  • cleanupTestCoroutines() → no longer needed (handled automatically)

Current code (app/unit-tests/src/org/commcare/rules/MainCoroutineRule.kt):

`@ExperimentalCoroutinesApi`
class MainCoroutineRule(val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher()) : TestWatcher() {
    override fun starting(description: Description) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }
    override fun finished(description: Description) {
        super.finished(description)
        Dispatchers.resetMain()
        testDispatcher.cleanupTestCoroutines()   // deprecated, no-op in new API
    }
    fun runBlockingTest(block: suspend () -> Unit) =
        this.testDispatcher.runBlockingTest { block() }  // deprecated
}

Scope: 2 files total — MainCoroutineRule.kt and LazyMediaDownloadTest.kt (the only consumer).

Proposed fix:

`@OptIn`(ExperimentalCoroutinesApi::class)
class MainCoroutineRule(val testDispatcher: TestDispatcher = StandardTestDispatcher()) : TestWatcher() {
    override fun starting(description: Description) {
        super.starting(description)
        Dispatchers.setMain(testDispatcher)
    }
    override fun finished(description: Description) {
        super.finished(description)
        Dispatchers.resetMain()
    }
}

And in LazyMediaDownloadTest.kt, replace mainCoroutineRule.runBlockingTest { ... } with runTest { ... } (from kotlinx.coroutines.test).

Why now: The deprecated APIs produce compiler warnings and will be removed in a future coroutines release. Fixing this keeps the test infrastructure clean and unblocks future coroutine test authors from inheriting the deprecated pattern.

This is a small, self-contained change with no functional impact on the tested behavior.

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@346204513ecfa08b81566450d7d599556807389f

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions