January 2019
Intermediate to advanced
392 pages
10h 11m
English
The runBlocking coroutine builder can be used for testing. This creates a coroutine that uses a current thread. The test within the JUnit framework may look as follows:
class ExampleUnitTest { @Test fun comicLoading() = runBlocking { val image = async { loadImage() }.await() assertNotNull(image) }}
This snippet loads an image using the async coroutine builder, and checks that the image is not null. The source code of the runBlocking function looks as follows:
@Throws(InterruptedException::class)public fun <T> runBlocking(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T { val currentThread = Thread.currentThread() val contextInterceptor = context[ContinuationInterceptor] val privateEventLoop ...
Read now
Unlock full access