Atul Gupta
09/18/2024, 8:56 AMandroidUnitTest
the source set, it launches a Composable which has commonMain/composeResources
usages. When running the TC I am getting below-error(the same TC runs successfully on androidTest
which uses real device)
Android context is not initialized. If it happens in the Preview mode then call PreviewContextConfigurationEffect() function.
Stacktrace originates from one of the usages of stringResource(Res.string.commonResourceName)
It seems that in the Roboelectric run, AndroidContextProvider
the content provider is not able to initialise the context for resource use.
Is there any workaround for the same?russhwolf
09/18/2024, 12:19 PMAtul Gupta
09/18/2024, 12:35 PMAtul Gupta
09/18/2024, 3:21 PMinternal class AndroidContextProvider : ContentProvider()
as that is internal ContentProvider
. Is there any api to init the context from the UT?Atul Gupta
09/18/2024, 4:10 PMCompositionLocalProvider(LocalInspectionMode provides true) {
PreviewContextConfigurationEffect()
}
before running the test, fix the issue. But this seems like a hack
Also after this fix now, kmm room setup crashing 😅 with the below error
no sqliteJni in java.library.path: /Users/user/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.:/Users/user/Learning/MonKey/MonKeyApp/src/testDebug/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/androidUnitTestDebug/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/test/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/androidUnitTest/jniLibs
java.lang.UnsatisfiedLinkError: no sqliteJni in java.library.path: /Users/user/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.:/Users/user/Learning/MonKey/MonKeyApp/src/testDebug/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/androidUnitTestDebug/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/test/jniLibs:/Users/user/Learning/MonKey/MonKeyApp/src/androidUnitTest/jniLibs
Atul Gupta
09/20/2024, 9:03 AMpitpit
09/25/2024, 1:37 PMAtul Gupta
09/25/2024, 1:38 PMPreviewContextConfigurationEffect
is composable function and defined at the same place where content provider is definedpitpit
09/25/2024, 1:39 PMAtul Gupta
09/25/2024, 1:40 PMpitpit
09/25/2024, 1:45 PMpitpit
09/25/2024, 1:51 PMAtul Gupta
09/25/2024, 1:55 PMpitpit
09/25/2024, 1:56 PMAtul Gupta
09/25/2024, 2:45 PMpitpit
09/25/2024, 3:12 PMAtul Gupta
09/25/2024, 3:15 PMcommonTest.dependencies {
// kotlin libs
implementation(libs.kotlin.test)
implementation(kotlin("test-annotations-common"))
// android
@OptIn(ExperimentalComposeLibrary::class)
implementation(compose.uiTest)
}
android {
testImplementation(libs.junit)
testImplementation(libs.androidx.compose.junit4)
// Needed for createComposeRule(), but not for createAndroidComposeRule<YourActivity>(): as this brings the androidx.activity.ComponentActivity for testing
debugImplementation(libs.androidx.compose.manifest)
testImplementation(libs.robolectric)
testImplementation(libs.koin.testJunit4)
}
this is mine setup.. I think some issue with your setup as some of the lib is not getting added to your projectpitpit
09/25/2024, 3:25 PMAtul Gupta
09/25/2024, 3:30 PMpitpit
09/25/2024, 4:00 PMPreviewContextConfigurationEffect() not resolved.
Atul Gupta
09/26/2024, 2:28 PMPreviewContextConfigurationEffect
) and see what is different. Also PreviewContextConfigurationEffect
method is only present in the Android source set so that you can only use this Android side of the codeAndrey Beryukhov
04/24/2025, 5:21 PMAtul Gupta
04/24/2025, 5:22 PMCompositionLocalProvider(LocalInspectionMode provides true) {
PreviewContextConfigurationEffect()
}
?Atul Gupta
04/24/2025, 6:07 PMAndrey Beryukhov
04/26/2025, 12:02 PMAndrey Beryukhov
04/26/2025, 2:30 PMsetupAndroidContextProvider
again.
And it worked with initialization of it before createAndroidComposeRule<MainActivity>()
.
Than I've got the same issue with no sqliteJni
in Room. Solved this by replacing database dependency with in-memory storage implementation by Koin.
Hope, this could help someone else. PR link.