After a recent migration to Dagger 2, the application I'm working on uses @ActivityScope for each function. Each application function is implemented using the MVP template and has its own setting for the local dagger component, which depends on the application component for the dependencies that are required throughout the application life cycle (provided by the application). Each Activity function extends a base class that provides the main component of the application with a method that is redefined by each action to configure the local component of the dagger (creates a local component and creates a local module).
The problem I'm trying to solve is how to inject mocks into the activity being tested. The main problem that I am facing is that I cannot change the source local component and the corresponding module with the mocked ones at runtime. There are many articles on espresso testing with dagger 2 that I read, but they do not promote pure architecture. For the most part, they rely on the AppComponent to input all actions, where in my case each component component is responsible for injecting its own activity.
Until now, the best approach I came up with was the introduction of a component builder, which is initialized only as part of the test installation, and in the action code, with this installation, if it is initialized, otherwise the real component is configured. However, I do not want to mix production and test codes. Here is a diagram that represents the dagger setting:
Dagger Setting 2
source
share