question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

please check this code. as I am trying to add the repository with @BindValue. the hilt is throwing error as follows ( issue found in hilt version 2.14)

See original GitHub issue

please check this code. as I am trying to add the repository with @BindValue. the hilt is throwing error as follows.


@HiltAndroidTest
@UninstallModules(AppModule::class)
@RunWith(AndroidJUnit4::class)
class RandomUserListFragmentTest {
    @get:Rule
    val hiltRule = HiltAndroidRule(this)

 @Inject
    lateinit var glideRequestManager: RequestManager

    @BindValue
    @JvmField  val repository: RandomUserRepositoryImplTest = mock()

  @Before
    fun launchFragment() {
        hiltRule.inject()
        ActivityScenario.launch(RandomUserActivity::class.java)
    }


   @Test
    fun verifyTheRecyclerViewHasData(){
        Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).check { view, noViewFoundException ->
            if (noViewFoundException != null) {
                throw noViewFoundException
            }

            val recyclerView = view as RecyclerView
            Assert.assertEquals(3, recyclerView.adapter?.itemCount)
        }
    }
}
@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [AppModule::class]
)
object FakeAppModuleTest {
    @Provides
    @Singleton
    fun initGlide(@ApplicationContext appContext: Context): RequestManager = Glide.with(appContext)
        .setDefaultRequestOptions(
            RequestOptions()
                .centerInside()
                .error(R.drawable.ic_user)
        )
}

getting errors like

Task :app:hiltJavaCompileDebugAndroidTest /Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/com/ss/assignment/ui/random_user_ui/RandomUserActivityTest_TestComponentDataSupplier.java:8: error: cannot find symbol import dagger.hilt.android.internal.testing.root.DaggerRandomUserActivityTest_HiltComponents_SingletonC; ^ symbol: class DaggerRandomUserActivityTest_HiltComponents_SingletonC location: package dagger.hilt.android.internal.testing.root /Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/com/ss/assignment/ui/random_user_ui/main_screen/RandomUserListFragmentTest_TestComponentDataSupplier.java:8: error: cannot find symbol import dagger.hilt.android.internal.testing.root.DaggerRandomUserListFragmentTest_HiltComponents_SingletonC; ^ symbol: class DaggerRandomUserListFragmentTest_HiltComponents_SingletonC location: package dagger.hilt.android.internal.testing.root /Users/shaheer/AndroidStudioProjects/ssassignment/app/build/generated/hilt/component_sources/debugAndroidTest/dagger/hilt/android/internal/testing/root/RandomUserActivityTest_HiltComponents.java:133: error: [Dagger/MissingBinding] com.ss.assignment.data.repository.RandomUserRepository cannot be provided without an @Provides-annotated method. public abstract static class SingletonC implements SunAndSandsApp_GeneratedInjector, ^ com.ss.assignment.data.repository.RandomUserRepository is injected at com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel(randomUserRepository) com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel is injected at com.ss.assignment.ui.random_user_ui.RandomUserSharedViewModel_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() [dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.SingletonC → dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.ActivityRetainedC → dagger.hilt.android.internal.testing.root.RandomUserActivityTest_HiltComponents.ViewModelC] 3 errors

Hilt version 2.14

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
sheiappcommented, Apr 26, 2022

Thank you so much @bcorso

1reaction
bcorsocommented, Apr 25, 2022

@sheiapp the entry point from the error message is coming from a component in RandomUserActivityTest, but the test in your snippet is RandomUserListFragmentTest. You will have to add the bindings to that test as well (though, if you end up doing this for every test you should also look into @TestInstallIn).

in the above solution, I saw you suggest to do a field injection but the actual injection is happening in ViewModel. will it make sense!.

It should be fine to inject RandomUserRepositoryImplTest into your test since the test is providing the binding into the SingletonComponent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

please check this code. as I am trying to add the repository ...
please check this code. as I am trying to add the repository with @BindValue. the hilt is throwing error as follows.
Read more >
Null dependency after migrating from Dagger to Hilt #3513
I'm doing a migration from Dagger (with dagger-android) to Hilt. After completing all the steps from the migration guide, I'm getting a Null ......
Read more >
Dagger Hilt in Android with Example - GeeksforGeeks
First, we will add the classpath to our project-level build.gradle file. For adding this dependency Go to Gradle Scripts > build.gradle(Project: ...
Read more >
Dependency injection with Hilt | Android Developers
The Hilt module AnalyticsModule is annotated with @InstallIn(ActivityComponent::class) because you want Hilt to inject that dependency into ExampleActivity .
Read more >
Testing with Hilt android - Nyame Bismark - Medium
Let's start talking about Hilt test. Adding Hilt Testing Dependencies to Gradle. To use Hilt in your tests, include the hilt-android-testing dependency in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found