vendredi 29 janvier 2021

Hilt BindValue and ActivityRetainedComponent not working in android instrumented tests

Hi everyone I am trying to use hilt in my instrumented test and even though the app is working fine the tests seem to have an issue with the dependencies Here is a link to my repo, and here is the test here is my test


@UninstallModules(WaterTrackingModule::class)
@HiltAndroidTest
class WaterTrackerTest {

    @get:Rule val hiltRule = HiltAndroidRule(this)


    @BindValue
    @JvmField val preferencesHelper = mock<PreferencesHelper>()

    @Before fun init() = hiltRule.inject()

    @Test
    fun waterCountDisplays() {
        val waterIntake = 5
        whenever(preferencesHelper.waterIntake())
            .doReturn(waterIntake)
        ActivityScenario.launch(WaterTrackerActivity::class.java)
        onView(withId(R.id.waterCountText))
            .check(matches(withText(waterIntake.toString())))
    }
}

and here is the WaterTrackingModule

@InstallIn(ActivityRetainedComponent::class)
@Module
class WaterTrackingModule {

    @ActivityRetainedScoped
    @Provides
    fun providesPreferencesHelper(
        @ApplicationContext context: Context
    ) = PreferencesHelper(context)
}

The test fails with this message

  public abstract static class SingletonC implements WaterTrackerApplication_GeneratedInjector,
                         ^
      com.abdelrahman.watertracker.PreferencesHelper is injected at
          com.abdelrahman.watertracker.WaterTrackingViewModel(preferencesHelper, …)
      com.abdelrahman.watertracker.WaterTrackingViewModel is injected at
          com.abdelrahman.watertracker.WaterTrackingViewModel_HiltModules.BindsModule.binds(arg0)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.abdelrahman.watertracker.WaterTrackerTest_HiltComponents.SingletonC → com.abdelrahman.watertracker.WaterTrackerTest_HiltComponents.ActivityRetainedC → com.abdelrahman.watertracker.WaterTrackerTest_HiltComponents.ViewModelC]
  It is also requested at:
      com.abdelrahman.watertracker.WaterCountTextView.preferencesHelper
  The following other entry points also depend on it:
      com.ab

Aucun commentaire:

Enregistrer un commentaire