When you use an @Inject-annotated constructor to inform Dagger how to provide a dependency, can you later override it in a Module? Case: my production code uses a component with the annotated constructor, and I want to override it with a test implementation during testing. Would my override = true Module successfully override the production implementation during testing?
Or a more general questions, what takes precedence, Modules or annotated classes?
@Provides methods always trump @Inject Constructors and no-args Constructors (with field injection.
This is important, because an @Provides method is taking over responsibility for creation of the type, and that includes scoping, so a class marked @Singleton that is manually constructed in an @Provides method will not be scoped unless the @Provides method is scoped.
Note that you're using the word "override" in the context of testing... in this case, you don't need to use the @Module(override=true) setting (though there's no harm). It's just a precedence order, which resolves (in effect) as follows:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With