kotlinx.coroutines
kotlinx.coroutines copied to clipboard
Dispatchers.setMain not working for 1.6+ versions
Currently, using 1.5.2, everything works fine.
After library update to 1.6.0 or 1.6.1 version, I'm receiving message Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used. Same message I receive, if I just remove @BeforeTest fun setup() { Dispatchers.setMain ... for 1.5.2.
Tried to add MainCoroutineRule - no use.
What could be a problem?
same here
Hi! This could be caused by a version mismatch, that is, if the kotlinx-coroutines-core version was increased, but the kotlinx-coroutines-test version wasn't.
If this is not the case, please provide an example of a project where this issue reproduces so I could take a look at it.
kotlinx-coroutines-core and kotlinx-coroutines-test using the same versions. Yeah it's different case i think
kotlinx-coroutines-android , kotlinx-coroutines-core and kotlinx-coroutines-test updated all together. Unfortunately, I can't share the project. I'll try to create a new one and step by step add using libraries to find out the problem. Return back with the result, but not sure when.
trying to add @RunWith(AndroidJUnit4::class) to my test class and then solved, may it can help if your problem same with me.
It would be really helpful if someone could add a reproducing project.
Mismatched versions shouldn't be an issue since #2952, so it's probably something else that we potentially can workaround on our side
If your class under test starts coroutines from its constructor and is constructed as a field in your test this will run before any Rule/ Before blocks.
class MyTest {
val classUnderTest = MyClass(TestScope()) //This runs before Rule/ Before
//Do Test
}
class MyClass(val scope: CoroutineScope) {
init {
scope.launch {...} //This now needs Main to be set when using TestScope
}
}
Simplest fix is to change to
val classUnderTest by lazy { MyClass(TestScope()) }
Any new update on the fix ? I have the same issue even if I add kotlinx-coroutines-android , kotlinx-coroutines-core and kotlinx-coroutines-test on the same project while writing and android instrumented test :/
We still need the reproducer in order to fix the issue, or at least to report it to the responsible party.
It works in general setups, but for some Android projects it doesn't, and we need a reproducer to pinpoint the issue
val classUnderTest by lazy { MyClass(TestScope()) }
This works for me. With
Dispatchers.setMain(Dispatchers.Unconfined)
Suggest to close this issue, because for my case issue is not relevant.