Skip to main content
Version: 1.0.0-beta10

Test Core

Test helpers for the dispatch-core module. Most of the tools you need to automatically handle DispatcherProvider in your tests. (see dispatch-test-junit4 or dispatch-test-junit5 for the rest.)

TestDispatcherProvider

Testing version of the DispatcherProvider with three main styles of creation:

Constructor with default arguments

(link)

Each property becomes its own TestCoroutineDispatcher by default, but may be replaced by any CoroutineDispatcher.

val customTestDispatcherProvider = TestDispatcherProvider(
default = newSingleThreadContext("default"),
io = newSingleThreadContext("io"),
main = newSingleThreadContext("main"),
mainImmediate = newSingleThreadContext("main immediate"),
unconfined = newSingleThreadContext("unconfined")
)

val defaultTetsDispatcherProvider = TestDispatcherProvider()

Single-arg factory

(link)

Another option is to pass a single CoroutineDispatcher, which is then used to populate all fields.

val dispatcher = newSingleThreadContext("custom")

val dispatcherProvider = TestDispatcherProvider(dispatcher)

dispatcherProvider.default shouldBe myDispatcher
dispatcherProvider.io shouldBe myDispatcher
dispatcherProvider.main shouldBe myDispatcher
dispatcherProvider.mainImmediate shouldBe myDispatcher
dispatcherProvider.unconfined shouldBe myDispatcher

Basic TestDispatcherProvider

(link)

Sometimes we want to have the normal dispatch behaviors of a production environment, just without the awkward mechanics of Dispatchers.setMain.

This is essentially DefaultDispatcherProvider except with a single-threaded executor handling the "main" thread.

fun TestBasicDispatcherProvider(): TestDispatcherProvider {

val mainThread = newSingleThreadContext("main thread proxy")

return TestDispatcherProvider(
default = Dispatchers.Default,
io = Dispatchers.IO,
main = mainThread,
mainImmediate = mainThread,
unconfined = Dispatchers.Unconfined
)
}

TestProvidedCoroutineScope

(link)

A polymorphic CoroutineScope which implements all the type-safe versions from dispatch-core, as well as TestCoroutineScope.

This type may be injected anywhere, regardless of the requirement.

Builders

Sometimes, instead of explicitly creating a CoroutineScope object, we prefer to just use a coroutineScope builder function within a function.

@Test
fun some_test() = runBlockingProvided {
someSuspendFunction()
}

@Test
fun some_test() = testProvided {
someSuspendFunctionWithADelay()
}
NameDescription
runBlockingProvidedUses runBlocking, but injects a DispatcherProvider into its CoroutineScope. Use this function if you want normal dispatch behavior.
testProvidedUses runBlockingTest, but injects a DispatcherProvider into its TestCoroutineScope. Use this function if you want the explicit time control of runBlockingTest.

Minimum Gradle Config

Add to your module's build.gradle.kts:

repositories {
mavenCentral()
}

dependencies {

// core
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2")
implementation("com.rickbusarow.dispatch:dispatch-core:1.0.0-beta10")

testImplementation("com.rickbusarow.dispatch:dispatch-test:1.0.0-beta10")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.2")
}