AndroidInjector and Espresso

Using AndroidInjector and subcomponents makes it impossible to inject the scope tested in the Test class for Espresso.

Previously, with application-level components and activity components, you had the option to call injection () for test classes that were not active until you created 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);
}

Testing 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);

, espresso UI Test? , "ServiceGateway" "NowPlayingPresenter" , . , . "NowPlayingPresenter", .

+4
2

Runner. , TestRunner Android. onCreate(), , .

0

. - 2 . Espresso.

, mcassiano, , . - , . , -, mcassiano. , ActivityTestRule Activity, , mocks.

2 :

  • ,
  • .

, , , , , .

, .

mocks, , , . , , , , , .

  • , , - , , .
  • , = espresso else -
0

Source: https://habr.com/ru/post/1676102/


All Articles