Creating Test Dependencies Using Dagger2

When reading documents for dagger 2, I cannot find a simple way to ensure addiction when creating an application for testing. The only key I found is:

Dagger 2 does not support overrides. Modules that overriding for simple fake testing can create a subclass of the module to emulate this behavior. Modules that use overrides and rely on dependency injection should be decomposed so that instead the modules are presented as a choice between two modules.

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

+10
android dagger dagger-2
Apr 30 '15 at 14:04
source share
1 answer

This is currently not possible with dagger 2 (starting with version 2.0) without any workarounds. You can read about it here .

I suggested one workaround , but this requires changes to the production code.

In short:

  • provide an extra set for @Component (e.g. in the Android setter in the Application class)
  • test component should expand production component

Check out both links for more information. I hope that this issue will be considered in future versions of dagger 2.

+11
May 05 '15 at 16:00
source share



All Articles