After updating coroutinesKotlinVersion to 1.7.1 , facing these issues while creating MainCoroutineRule with UnconfinedTestDispatcher()
Could not initialize class kotlinx.coroutines.test.TestCoroutineScheduler
java.lang.NoClassDefFoundError: Could not initialize class kotlinx.coroutines.test.TestCoroutineScheduler
at kotlinx.coroutines.test.TestCoroutineDispatchersKt.UnconfinedTestDispatcher(TestCoroutineDispatchers.kt:87)
at kotlinx.coroutines.test.TestCoroutineDispatchersKt.UnconfinedTestDispatcher$default(TestCoroutineDispatchers.kt:83)
at net.example.cellfish.MainCoroutineRule.<init>(MainCoroutineRule.kt:12)
at net.example.cellfish.search.viewmodel.SearchTransactionHistoryViewModelTest.<init>(SearchTransactionHistoryViewModelTest.kt:49)
If I downgrade the version to 1.6.4, test cases seems to be working fine. Able to initlise MainCoroutineRule.
For Ref-
@OptIn(ExperimentalCoroutinesApi::class)
class MainCoroutineRule constructor(
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
) : TestWatcher() {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}
override fun finished(description: Description) {
super.finished(description)
Dispatchers.resetMain()
}
}
Dependencies for kotlin coroutine
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1"
So figured out that internally kotlinx-coroutines-test:1.7.1
is trying to use kotlin-stdlib:1.8.10
. But coz of our usecase we were forcing any kotlin based library to use 1.7.20 and so making kotlinx-coroutines-test:1.7.1
to use kotlin-stdlib:1.7.20
as well . It it seems like its some compatibility issue (backward compatibility) with kotlinx-coroutines-test:1.7.1
for old kotlin version.