Skip to content

Latest commit

 

History

History
51 lines (38 loc) · 1.59 KB

File metadata and controls

51 lines (38 loc) · 1.59 KB

Module kotlinx-coroutines-test

Test utilities for kotlinx.coroutines. Provides Dispatchers.setMain to override Main dispatcher.

Using in your project

Add kotlinx-coroutines-test to your project test dependencies:

dependencies {
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.1.0'
}

Do not depend on this project in your main sources, all utilities are intended and designed to be used only from tests.

Once you have it in runtime, ServiceLoader mechanism will overwrite Dispatchers.Main will testable implementation.

You can override this implementation using [setMain][Dispatchers.setMain] method with any CoroutineDispatcher implementation, e.g.:

class SomeTest {
    
    private val mainThreadSurrogate = newSingleThreadContext("UI thread")

    @Before
    fun setUp() {
        Dispatchers.setMain(mainThreadSurrogate)
    }

    @After
    fun tearDown() {
        Dispatchers.resetMain() // reset main dispatcher to original Main dispatcher
        mainThreadSurrogate.close()
    }
    
    @Test
    fun testSomeUI() = runBlocking {
        launch(Dispatchers.Main) {  
            ...
        }
    }
}