0

I'm was starting to using hilt and I ran into a problem while testing.

This is the code that is generating the following error:

@HiltAndroidTest
@RunWith(CommonUiRunner::class)
class PhoneEditTextTest {

    @Test
    fun testThatNumberFormatted() {
        val controller = Robolectric.buildActivity(AppCompatActivity::class.java)
        val activity = controller.get()
        val editText = PhoneEditText(activity)

        activity.setContentView(editText)
        controller.setup()
    }
}

@Module
@InstallIn(ApplicationComponent::class)
object ApplicationModule {

    @Provides
    @Singleton
    fun providePhoneNumberUtil(app: Application): PhoneNumberUtil = PhoneNumberUtil.createInstance(AssetsMetadataLoader(app.assets))
}

@Module
@InstallIn(ApplicationComponent::class)
object CountriesFeatureModule {

    @Provides
    @Singleton
    fun providesApi(app: Application): CountriesFeatureApi = CountriesFeatureImpl(app.assets)
}

Returning:

APPLICATION and COUNTRIES-FEATURE modules contains in INJECTOR gradle-module.

Do you know how to fix this?

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47

1 Answers1

0

Yes, you cannot use normal Activities when you want to test dagger-hilt stuff.

Please look at the following tutorial as it should solve your problem: https://www.youtube.com/watch?v=k4zG93ogWFY&t

Andrew
  • 4,264
  • 1
  • 21
  • 65