热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Dagger2进行单元测试中的现场注入-FieldinjectioninunittestswithDagger2

AsadvisedinDaggerdocumentation,forunittestingwedonthavetoinvolveDaggeratall,andfor

As advised in Dagger documentation, for unit testing we don't have to involve Dagger at all, and for the provided example it makes sense:

正如Dagger文档中所建议的那样,对于单元测试,我们根本不需要涉及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) { /* … */ }
}

With this class structure it is simple to unit test, just mock the getter and putter, pass them as constructor arguments, instruct mockito what to return when interacting with any of these objects, and then make assertions on doTheThing(...).

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

Where I am struggling at testing is when I have to unit test a class with a structure similar to this:

我在测试中苦苦挣扎的地方是我必须对类似于以下结构的类进行单元测试:

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) { /* … */ }
}

As you can see, I am no longer using constructor injection, but field injection instead. The main reason is not have too many parameters in the constructor.

如您所见,我不再使用构造函数注入,而是使用场注入。主要原因是构造函数中没有太多参数。

Is there a way to inject mock dependencies in ThingDoer when all its dependencies are provided using field injections?

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

1 个解决方案

#1


0  

For field injection, you can create a component and a module which are used in unit test.

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

Suppose you have the unit test class ThingDoerTest, you can make the component injects dependencies to ThingDoerTest instead ThingDoer and the module provides the mock object instead real object.

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

In my project, HomeActivity has a field injection HomePresenter. Following code are some snippets. Hope the code can give you some idea.

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

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

    @Rule
    public InjectorActivityTestRule 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);
        }
    }

}

推荐阅读
author-avatar
速度coinmer
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有