dispatch-test

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.)

Contents

  • #testdispatcherprovider

    • #constructor-with-default-arguments

    • #single-arg-factory

    • #basic-testdispatcherprovider

  • #testprovidedcoroutinescope

  • #builders

    • #minimum-gradle-config

TestDispatcherProvider

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

Constructor with default arguments

TestDispatcherProvider

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

TestDispatcherProvider

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

TestDispatcherProvider

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

TestProvidedCoroutineScope

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.

Packages

Link copied to clipboard