How to use the inMemory() setting?

When I try to use the inMemory() setting, I run into various problems.

Roughly it looks like this:

RealmConfiguration.Builder(
schema = setOf(
…RealmObject declarations
)
).inMemory().build()

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.

What am I doing wrong?

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.

We are in the process of migrating to the new Android source set layout from Kotlin 1.8 and you can track the progress in Migrate to new Android source sets for tests introduced in Kotlin 1.8 by cmelchior · Pull Request #1399 · realm/realm-kotlin · GitHub.

Until that you will have to run Android tests (both unit tests and device test) on a device/emulator.

That’s what I’m trying to do. That’s where I got the second exception.

I’m working on an Android only project, so the ticket is not relevant to me, I think.

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)

And it works! Thanks.

Great to hear.

… Claus