Wrong application instance from ApplicationProvider.getApplicationContext

206 views Asked by At

I am providing my own implementation of Application by creating a subclass and specifying the fully-qualified name of this subclass as the android:name attribute in AndroidManifest.xml's <application> tag as:

<application
        android:name=".GeniuzApp"
...

Running this Android App I can get the correct Application object in an Activity such as: val app = application as GeniuzApp

Yet, when I call ApplicationProvider.getApplicationContext in a unit test, which is configured with @RunWith(RobolectricTestRunner::class), I am not getting an instance of my Application subclass (i.e. GeniuzApp).

Do I need to provide anything else on my unit test to get the correct application instance?

1

There are 1 answers

0
Miguel Gamboa On

I was missing the following in build.gradle:

android {
  testOptions {
    unitTests {
      includeAndroidResources = true
    }
  }
}

as stated in Building with Android Studio/Gradle