Sunday, 28 November 2021

Android Instrumented tests with KodeIn

We have an Android app that is using compose for the view layer and we are using Kodein for all of our dependency injections.

I have a BaseApplication class which is DIAware:

class BaseApplication : Application(), DIAware {
    override val di: DI = DI.lazy {
        import(modules) // modules defined in respective packages
    }
}

I also have a MainActivity and a nav graph to manage navigation between the various composables.

Problem: How can I properly override these modules in my instrumented tests for MainActivity?

@RunWith(AndroidJUnit4::class)
class MainActivityTest {
    @get:Rule
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    val moduleOverrides = DI.Module(allowSilentOverride = true) {
        // add bindings for fakes w/ allowOverride = true
    }
    
    @Before
    fun setup() {
        val application =
            ApplicationProvider.getApplicationContext() as BaseApplication

        // how can I override the BaseApplication modules for my test?
    }

}

I cant seem to find any clear language on the matter and feel like I am missing something very obvious. Any help would be very much appreciated.



from Android Instrumented tests with KodeIn

No comments:

Post a Comment