In Unit tests it causes an ExceptionInInitializerError with NullPointerException.
In Robolectric or Instrumentation tests it causes the following:
kotlin.UninitializedPropertyAccessException: lateinit property filesDir has not been initialized.
The issue is that Android unittests are running in a non-Android JVM environment, but the infrastructure in AGP is pulling in Android dependencies for the test. The dependencies are not properly initialized when not running on an device/emulator.
Hmm, okay. Then it might be that our initializer, that picks up the context, just isn’t triggered correctly. We use the App Startup infrastruture as described in App Startup | Android Developers for that. Might be that Robolectric has an initializer that is running before ours. Would you be able to inspect the app manifest to see if Robolectric involves any initializers (App Startup | Android Developers). If so then we might be able to control the ordering of them by providing a custom initializer that depends on both.
That makes sense now, because we use Initializers, and when I set up the project, I had to add RealmInitializer as a dependency for a few Initializers to make it work and remove the very same error message with the filesDir.
However, now I run into this in normal Ui tests as well, even when I turn HiltAndroidRule off. Do I need to pass context somehow in tests?
I think I solved it. We still used (even with Hilt off) a custom AndroidJUnitRunner with a custom Application. I added RealmInitializer to this like this:
val appInitializer = AppInitializer.getInstance(this)
appInitializer.initializeComponent(RealmInitializer::class.java)
My last comment above solved the problems with Instrumentation tests. Now as the last part of our migration, I delete SQLite DataSources and convert their Robolectric integration Unit tests to target the Realm DataSources.
However, I run into a similar problem as before. Here is the stack trace:
com.getkeepsafe.relinker.MissingLibraryException: Could not find 'librealmc.dylib'. Looked for: [armeabi-v7a], but only found: [].
androidx.startup.StartupException: com.getkeepsafe.relinker.MissingLibraryException: Could not find 'librealmc.dylib'. Looked for: [armeabi-v7a], but only found: [].
at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:187)
at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:138)
at androidx.startup.AppInitializer.initializeComponent(AppInitializer.java:117)
at com.reachplc.data.news.InstrumentationTestApp.onCreate(InstrumentationTestApp.kt:15)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
at org.robolectric.android.internal.RoboMonitoringInstrumentation.callApplicationOnCreate(RoboMonitoringInstrumentation.java:127)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$installAndCreateApplication$2(AndroidTestEnvironment.java:368)
at app//org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:86)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:366)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$createApplicationSupplier$0(AndroidTestEnvironment.java:245)
at app//org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$createApplicationSupplier$1(AndroidTestEnvironment.java:242)
at com.google.common.base.Suppliers$NonSerializableMemoizingSupplier.get(Suppliers.java:183)
at org.robolectric.RuntimeEnvironment.getApplication(RuntimeEnvironment.java:72)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:210)
at app//org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:331)
at app//org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$2(SandboxTestRunner.java:278)
at app//org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:99)
at java.base@17.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base@17.0.5/java.lang.Thread.run(Thread.java:833)
Caused by: com.getkeepsafe.relinker.MissingLibraryException: Could not find 'librealmc.dylib'. Looked for: [armeabi-v7a], but only found: [].
at com.getkeepsafe.relinker.ApkLibraryInstaller.installLibrary(ApkLibraryInstaller.java:175)
at com.getkeepsafe.relinker.ReLinkerInstance.loadLibraryInternal(ReLinkerInstance.java:180)
at com.getkeepsafe.relinker.ReLinkerInstance.loadLibrary(ReLinkerInstance.java:136)
at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:70)
at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:57)
at io.realm.kotlin.internal.AndroidUtilsKt.loadAndroidNativeLibs(AndroidUtils.kt:14)
at io.realm.kotlin.internal.RealmInitializer.create(RealmInitializer.kt:42)
at io.realm.kotlin.internal.RealmInitializer.create(RealmInitializer.kt:30)
at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:180)
at androidx.startup.AppInitializer.doInitialize(AppInitializer.java:138)
at androidx.startup.AppInitializer.initializeComponent(AppInitializer.java:117)
at com.reachplc.data.news.InstrumentationTestApp.onCreate(InstrumentationTestApp.kt:15)
at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callApplicationOnCreate(Instrumentation.java:1154)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java)
... 17 more
Important to note, that we have normal Unit tests with mocked Realm, but these are integration tests with in-memory Realm.
I found some related questions on the internet, but they are mostly old and probably outdated. I found this ticket too, which is outstanding, but this is for realm-java, and it seems there is no more blockers to add Robolectric support:
So ultimately the question is: is Robolectric supported for in-memory Realm for realm-kotlin?