简体   繁体   中英

dynamic binding with dagger 2

I have problems migrating my Android app from Dagger to Dagger2 .

I use Dagger to inject mocks at different layers during testing (unit , functional device and everything in between).

Eg I have a NetworkingModule which provides a HttpTransport and a ServerConnector . For some test I just mock the http layer, for others I mock the ServerConnector , and other test use release classes to test integration with a real test server.

These test can be run on all build flavors (debug, release, ...).

I found only descriptions of Dagger2 which set a single static injection configuration per build type - ie a release component to bind real classes and a debug component to bind one set of mocks.

I need to find a way to have the test cases replace modules in the application start up phase.

How can I achieve this? In my thoughts I always get stuck when the application triggers the building of the graph for which it has to use a Component which is the implementation of a certain interface which has static annotation of its modules???

To ask differently: Is it possible to compile multiple modules in the same build which provide the same injections? How would I then inject with the right module at run time?

Can someone please draft me a solution for my example of the NetworkingModule .

BTW: I make heavy use of Espresso , Robolectric , and Mockito .

Thx.

Found a solution:

I don't need build specific Components or Modules.

My modules just handle delegates which I implement and set for injecting mocks when testing. This is very cheap for release builds. And for testing I can even effort some reflection for convenience features.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM