Alexandru Hadăr
11/25/2021, 3:19 PMclass Launcher(dispatcher: CoroutineDispatcher) {
private val scope = CoroutineScope(SupervisorJob() + dispatcher)
fun launchItem() {
scope.launch { /* Do my thing here */ }
}
}
I suppose the correct way to test this is to use Dispatchers.setMain(myDispatchers) in the @Before function and then use runBlocking in my test function, where I call launcher.launchItem , right?
Or how should I test a method that’s not suspend but launches a coroutine inside ?Joffrey
11/25/2021, 3:23 PMcoroutineContext instead of just the dispatcher in this kind of classes. This way you can make the job a child of your test's coroutineContext . Otherwise runBlocking and the likes will not wait for your coroutine to finish.