Injection properties like this are not so easy to verify. In this case, the constructor assembly is much better. Refactoring your constructor is as follows:
private final RssService rssService; @Inject public RssListPresenter(RssService rssService) { this.rssService = rssService; }
Now you can easily test it:
//mocks RssService mockRssService; //system under test RssListPresenter rssListPresenter; @Before public void setup() { mockRssService = Mockito.mock(RssService.class); rssListPresenter = new RssListPresenter(mockRssService); }
You should probably not use DaggerNetworkComponent.inject(this) inside RssListPresenter . Instead, you should configure the dagger so that when you introduce members to the top-level classes ( Activity , Fragment , Service ), he can access the graph of objects and create an instance of your RssPresenter .
Why just put the injectors in Activity and Service , and not something like RssListPresenter ? These are classes that are created by the Android system, so you have no choice but to use nozzles.
To clarify, Activity , Fragment , etc. are ideal injection targets . RssListPresenter etc. nested dependencies . You need to configure the dependency of the framework , the dagger so that it can provide the correct dependencies for the injection for the purpose of the injection.
So you also need to write the @Provides method for RssListPresenter
@Provides provideRssListPresenter(RssService rssService) { return new RssListPresenteR(rssService); }
source share