Using AndroidInjector and Subcomponents make it impossible to inject the activity scoped objected into the Test class for Espresso.
Previously with Application level components and activity components you had the ability call inject() for test classes that were not activities as long as you create a test component that inherited the activity component.
Example:
Activity Component
@ActivityScope
@Component(
dependencies = ApplicationComponent.class,
modules = {
NowPlayingActivityModule.class
})
public interface NowPlayingActivityComponent {
void inject(NowPlayingActivity activity);
}
Test Class Component
@ActivityScope
@Component(
dependencies = TestApplicationComponent.class,
modules = {
TestNowPlayingActivityModule.class,
ActivityModule.class
})
public interface TestNowPlayingActivityComponent extends NowPlayingActivityComponent {
void inject(NowPlayingActivityTest nowPlayingActivityTest);
}
Test Module
@Module
public class TestNowPlayingActivityModule {
private NowPlayingActivityModule nowPlayingActivityModule;
public TestNowPlayingActivityModule(NowPlayingActivityModule nowPlayingActivityModule) {
this.nowPlayingActivityModule = nowPlayingActivityModule;
}
@Provides
@ActivityScope
public ServiceGateway providesServiceGateway(ServiceApi serviceApi) {
return nowPlayingActivityModule.providesServiceGateway(serviceApi);
}
@Provides
@ActivityScope
public NowPlayingPresenter providesNowPlayingPresenter(NowPlayingInteractor nowPlayingInteractor) {
//In order to make sure espresso idles the view checks, we put the IdlingResource on the presenter.
return Mockito.spy(new NowPlayingPresenterImpl_IdlingResource(nowPlayingActivityModule.getNowPlayingViewModel(),
nowPlayingInteractor));
}
}
In Test Class
TestNowPlayingActivityComponent mockNowPlayingActivityComponent = DaggerTestNowPlayingActivityComponent.builder()
.testApplicationComponent((TestApplicationComponent) mvpExampleApplication.getComponent())
.testNowPlayingActivityModule(new TestNowPlayingActivityModule(nowPlayingActivityModule))
.build();
mockNowPlayingActivityComponent.inject((NowPlayingActivity) activity);
mockNowPlayingActivityComponent.inject(NowPlayingActivityTest.this);
How are people getting access to the activity modules that are auto generated and using them in espresso UI Test? I want to have access to objects like "ServiceGateway" & "NowPlayingPresenter" above and utilize them in the test. Either mock, spy, or idling resource. My idling resource in the above example is the "NowPlayingPresenter" concrete implementation that I pass to espresso during each individual test.