使用Dagger 2进行单元测试中的现场注入

时间:2017-01-05 12:31:22

标签: android unit-testing dagger-2

正如Dagger documentation所述,对于单元测试,我们根本不需要涉及Dagger,并且对于提供的示例,它是有道理的:

class ThingDoer {
  private final ThingGetter getter;
  private final ThingPutter putter;

  @Inject ThingDoer(ThingGetter getter, ThingPutter putter) {
    this.getter = getter;
    this.putter = putter;
  }

  String doTheThing(int howManyTimes) { /* … */ }
}

使用这个类结构,单元测试很简单,只需模拟getterputter,将它们作为构造函数参数传递,指示mockito与任何这些对象交互时返回什么,然后在doTheThing(...)上做出断言。

我在测试中苦苦挣扎的是当我必须对具有类似结构的类进行单元测试时:

class ThingDoer {
    @Inject
    ThingGetter getter;

    @Inject
    ThingPutter putter;

    @Inject
    ThingMaker maker;

    @Inject
    // other 10 objects

    public ThingDoer() {
        App.getThingComponent().inject(this);
    }

    String doTheThing(int howManyTimes) { /* … */ }
}

正如您所看到的,我不再使用构造函数注入,而是使用字段注入。主要原因是构造函数中没有太多参数。

当使用字段注入提供所有依赖项时,有没有办法在ThingDoer中注入模拟依赖项?

1 个答案:

答案 0 :(得分:0)

对于现场注入,您可以创建在单元测试中使用的组件和模块。

假设你有单元测试类ThingDoerTest,你可以让组件将依赖项注入ThingDoerTest而不是ThingDoer,模块提供模拟对象而不是真实对象。

在我的项目中,HomeActivity有一个字段注入HomePresenter。以下代码是一些片段。希望代码可以给你一些想法。

@RunWith(AndroidJUnit4.class)
public class HomeActivityTest implements ActivityLifecycleInjector<HomeActivity>{

    @Rule
    public InjectorActivityTestRule<HomeActivity> activityTestRule = new InjectorActivityTestRule<>(HomeActivity.class, this);

    @Inject
    public HomePresenter mockHomePresenter;

    @Override
    public void beforeOnCreate(HomeActivity homeActivity) {
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        MyApplication myApplication = (MyApplication) instrumentation.getTargetContext().getApplicationContext();

        TestHomeComponent testHomeComponent = DaggerHomeActivityTest_TestHomeComponent.builder()
            .appComponent(myApplication.getAppComponent())
            .mockHomeModule(new MockHomeModule())
            .build();
        testHomeComponent.inject(HomeActivityTest.this);
        homeActivity.setHomeComponent(testHomeComponent);
    }

    @Test
    public void testOnCreate() throws Exception {
        verify(mockHomePresenter).start();
    }

    @ActivityScope
    @Component(
        dependencies = {
            AppComponent.class
        },
        modules = {
            MockHomeModule.class
        }
    )
    public interface TestHomeComponent extends HomeComponent {
        void inject(HomeActivityTest homeActivityTest);
    }

    @Module
    public class MockHomeModule {
        @ActivityScope
        @Provides
        public HomePresenter provideHomePresenter() {
            return mock(HomePresenter.class);
        }
    }

}