I am trying to run the RoboElectric test in `andro...
# multiplatform
a
I am trying to run the RoboElectric test in
androidUnitTest
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)
Copy code
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?
r
This isn't a multiplatform issue. Robolectric doesn't automatically initialize ContentProviders so you need to do it manually.
a
Thanks for the update. I think Roboelectric issue id will be https://github.com/robolectric/robolectric/issues/8461
Though not sure how to initialise the
internal class AndroidContextProvider : ContentProvider()
as that is internal
ContentProvider
. Is there any api to init the context from the UT?
Doing below
Copy code
CompositionLocalProvider(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
Copy code
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
p
where is the class PreviewContextConfigurationEffect ?
a
PreviewContextConfigurationEffect
is composable function and defined at the same place where content provider is defined
p
I've got error unresolved reference
a
You might be using the old compose ui lib
p
I don't find it. In which package is the class ?
p
I've got 1.7.0-beta2 and i don't have this class
a
This is for KMM compose project, my compose version is same
p
I'm on KM project too
p
humm I don't understand i see the class in beta-02 org.jetbrains.compose.componentscomponents resources android1.7.0-beta02 but when i added implementation in androidUnitTest for my robolectric unit test I can't import it
a
Copy code
commonTest.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 project
p
Your unit test is into commonTest or androidUnitTest ?
a
I have in both, I couldn’t run compose in androidUnitTest due to above issue(in my case room is also crashing). I have then switched to commonTest and androidInstrumentedTest(for navigation as navigation testing lib is not currently KMM) for compose test
p
my test is only inCommonTest and use robolectric for androidUnitTest and with 1.7.0-beta02 of compose plugin is ok only for androidInstrumentedTest. KO for androidUnitTest (with Robolectric). With compose plugin 1.6.11 all tests passed. I put libs like you and 1.7.0-beta02 but
PreviewContextConfigurationEffect() not resolved.
a
You can try this project(it is somewhat minimal with call to
PreviewContextConfigurationEffect
) 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 code
a
@Atul Gupta did you solved this issue?
a
Is below code not working
Copy code
CompositionLocalProvider(LocalInspectionMode provides true) {
    PreviewContextConfigurationEffect()
}
?
a
https://kotlinlang.slack.com/archives/C3PQML5NU/p1745515351281299?thread_ts=1726649785.992159&amp;cid=C3PQML5NU Thank you To be honest, I didn't checked exactly this, because I have an Android application in stage of migration to Compose from View. And my main purpose was to try launch existing UI tests on Robolectric (JVM) instead of emulators. As there are multiple entrypoints into Compose world, I find it difficult to put this code to all of that places. But that is only my problem for sure 🙂
After your answer, I tried to apply the fix from the issue with
setupAndroidContextProvider
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.
163 Views