10

While reading the docs for dagger 2 I cannot find an easy way to provide a dependency when building an app for testing. The only clue I've found is this:

Dagger 2 doesn't support overrides. Modules that override for simple testing fakes can create a subclass of the module to emulate that behaviour. Modules that use overrides and rely on dependency injection should be decomposed so that the overridden modules are instead represented as a choice between two modules.

I don't understand how I would set up such a configuration on Android, anyone can explain?

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40

1 Answers1

12

This is currently impossible with Dagger 2 (as of v2.0.0) without some workarounds. You can read about it here.

I've proposed one workaround but this requires changes to the production code.

In short:

  • provide additional setter for @Component (e.g. in Android setter in Application class)
  • test component must extend the production component

For more information please check both links. Hope this issue will be addressed in future versions of Dagger 2.

tomrozb
  • 25,773
  • 31
  • 101
  • 122