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.

Bound Multiple Times issue when using Hilt for UI Test, TestInstalln

See original GitHub issue

Hello, I’ve created a fake urlprovider binding and annotated with @TestInstallIn

@Module
@TestInstallIn(
    components = [SingletonComponent::class],
    replaces = [UrlModuleBindings::class]
)
abstract class FakeUrlBindings {

    @Binds
    abstract fun bindsUrlProvider(urlProvider: FakeUrlProvider): UrlProvider
}

This is inside androidTest my Prod UrlProvider looks like

@Module
@InstallIn(SingletonComponent::class)
abstract class UrlModuleBindings {

    @Binds
    abstract fun bindsUrlProvider(urlProvider: DefaultUrlProvider): UrlProvider
}

This is Original Binding when I’m running the UI test getting the error **UrlProvider is bound multiple times:**

As per the documentation everything seems correct, can I please know what is the issue with this.

Have followed https://dagger.dev/hilt/testing.html

https://stackoverflow.com/questions/70844153/bound-multiple-times-issue-when-using-hilt-for-ui-test-testinstalln

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
bcorsocommented, Feb 13, 2022

Hi @ankitkhare,

Sorry for the delay!

To fix this, you need to remove the @Module.includes from AppModule.

- @Module(includes = [UrlModuleBindings::class, NetworkModule::class])
+ @Module
@InstallIn(SingletonComponent::class)
object AppModule {
  // ...
}

The issue is that while Hilt does uninstall the explicit UrlModuleBindings module usage in the generated code, it’s still transitively included by AppModule.

In Hilt, you shouldn’t ever need to use @Module.includes unless you’re including a non-Hilt module that you can’t use @InstallIn on directly. In this case, UrlModuleBindings already has @InstallIn on it so it shouldn’t be included by another module.

1reaction
bcorsocommented, Feb 9, 2022

Thanks for the sample project @ankitkhare!

I was able to repro the issue in your project, however it may take me a few days before I can start debugging and fixing it. I’ll update here once I know something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bound Multiple Times issue when using Hilt for UI Test ...
when I'm running the UI test getting the error UrlProvider is bound multiple times: As per the documentation everything seems correct, can I ......
Read more >
Hilt testing guide | Android Developers
Hilt isn't necessary for unit tests, since when testing a class that uses constructor injection, you don't need to use Hilt to instantiate...
Read more >
egghead-next | The frontend for egghead.io | UI Testing library - kandi
Implement egghead-next with how-to, Q&A, fixes, code snippets. kandi ratings ... Bound Multiple Times issue when using Hilt for UI Test, TestInstalln.
Read more >
Hilt Testing Best Practices in the MAD Skills series - Medium
This time we'll be focusing on how to write tests with Hilt and some of the best practices to be aware of. Since...
Read more >
Newest 'android-instrumentation' Questions - Stack Overflow
Bound Multiple Times issue when using Hilt for UI Test, TestInstalln. I've created a fake urlprovider binding and annotated with TestInstallIN @Module ...
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