DaggerMock 使用文档

作者: binwin20 | 来源:发表于2016-07-30 23:06 被阅读197次

    原文

    DaggerMock 是一个 JUnit rule, 方便覆盖 Dagger2 的 dependency

    更多关于 Dagger2 和 Mockito 测试的文章可以查看 Medium Post

    覆盖一个 Dagger2 创建的 dependency 是很麻烦的, 你需要定义一个 TestModule. 如果你想注入一个用于测试的 dependency, 还需要定义一个 TestComponent.

    使用 DaggerMockRule, 你能够更方便地覆盖一个由 Dagger2 的 module 生成的 dependency:

    public class MainServiceTest {
    
        @Rule public DaggerMockRule<MyComponent> rule = new DaggerMockRule<>(MyComponent.class, new MyModule())
                .set(new DaggerMockRule.ComponentSetter<MyComponent>() {
                    @Override public void setComponent(MyComponent component) {
                        mainService = component.mainService();
                    }
                });
    
        @Mock RestService restService;
    
        @Mock MyPrinter myPrinter;
    
        MainService mainService;
    
        @Test
        public void testDoSomething() {
            when(restService.getSomething()).thenReturn("abc");
    
            mainService.doSomething();
    
            verify(myPrinter).print("ABC");
        }
    }
    

    DaggerMockRule rule 实例化的时候, 它会查找测试类中的 @Mock 注解的字段, 如果这个字段在 module 中有提供, 则为这个提供的对象创建一个 mock 对象并注入到这个字段.

    MyModule 中提供了 RestServiceMyPrinter 两个 dependency. 在幕后, DaggerMockRule rule 会创建一个新的 MyModule 覆盖原来的 module, 然后返回 MyPrinterRestService 的 mock 对象, 如下:

    public class TestModule extends MyModule {
        @Override public MyPrinter provideMyPrinter() {
            return Mockito.mock(MyPrinter.class);
        }
    
        @Override public RestService provideRestService() {
            return Mockito.mock(RestService.class);
        }
    }
    

    DaggerMock 只能覆盖 Dagger 中使用 module 提供的 dependency, 不能覆盖使用 Inject 定义的对象. 0.6 版本之后, 如果使用了 Inject 定义的对象就报错 runtime error.

    支持 Espresso

    DaggerMockRule 可以用在 Espresso 中:

    public class MainActivityTest {
    
        @Rule public DaggerMockRule<MyComponent> daggerRule = new DaggerMockRule<>(MyComponent.class, new MyModule())
                .set(new DaggerMockRule.ComponentSetter<MyComponent>() {
                    @Override public void setComponent(MyComponent component) {
                        App app = (App) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
                        app.setComponent(component);
                    }
                });
    
        @Rule public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, false, false);
    
        @Mock RestService restService;
    
        @Mock MyPrinter myPrinter;
    
        @Test
        public void testCreateActivity() {
            when(restService.getSomething()).thenReturn("abc");
    
            activityRule.launchActivity(null);
    
            verify(myPrinter).print("ABC");
        }
    }
    

    支持 Robolectric

    类似的, 可以在 Robolectric 中使用:

    @RunWith(RobolectricGradleTestRunner.class)
    @Config(constants = BuildConfig.class, sdk = 21)
    public class MainActivityTest {
    
        @Rule public final DaggerMockRule<MyComponent> rule = new DaggerMockRule<>(MyComponent.class, new MyModule())
                .set(new DaggerMockRule.ComponentSetter<MyComponent>() {
                    @Override public void setComponent(MyComponent component) {
                        ((App) RuntimeEnvironment.application).setComponent(component);
                    }
                });
    
        @Mock RestService restService;
    
        @Mock MyPrinter myPrinter;
    
        @Test
        public void testCreateActivity() {
            when(restService.getSomething()).thenReturn("abc");
    
            Robolectric.setupActivity(MainActivity.class);
    
            verify(myPrinter).print("ABC");
        }
    }
    

    InjectFromComponent 注解

    在第一个样例中, 我们使用 ComponentSetter 把 component 中的 dependency 注入到字段中:

    @Rule public DaggerMockRule<MyComponent> rule = new DaggerMockRule<>(MyComponent.class, new MyModule())
            .set(new DaggerMockRule.ComponentSetter<MyComponent>() {
                @Override public void setComponent(MyComponent component) {
                    mainService = component.mainService();  // 注入到 mainService
                }
            });
    
    MainService mainService;
    

    0.6 版本之后, 我们可以使用 InjectFromComponent 获取 dependency

    public class MainServiceTest {
    
        @Rule public final DaggerMockRule<MyComponent> rule = new DaggerMockRule<>(MyComponent.class, new MyModule());
    
        @Mock RestService restService;
    
        @Mock MyPrinter myPrinter;
    
        @InjectFromComponent MainService mainService;
    
        @Test
        public void testDoSomething() {
            when(restService.getSomething()).thenReturn("abc");
    
            mainService.doSomething();
    
            verify(myPrinter).print("ABC");
        }
    }
    

    注: @Mock 是 DaggerMock 创建的 Module 提供的 mock 对象, @InjectFromComponent 是原 component 或者原 module 提供的对象, 一般用于注入被测试的对象.

    很多 Dagger 提供的 dependency 是不能通过 component 获取到的, 这时候可以使用下面的方式获取:

    @InjectFromComponent(MainActivity.class) MainService mainService;
    @InjectFromComponent({MainActivity.class, MainPresenter.class}) MainService mainService;
    

    注: 查看 DaggerMockRule 可知, InjectFromComponent 实现原理是: 1. 没有参数时, 从 component 或者 module 中获取; 2. 有参数时, 如上面例子2, 用反射创建一个对象 MainActivity, 获取 MainActivity 中的字段 MainPresenter, 获取 MainPresenter 中的字段 MainService, 赋值给 mainService

    自定义规则

    可以创建一个 MyRule 继承自 DaggerMockRule 复用代码

    public class MyRule extends DaggerMockRule<MyComponent> {
        public MyRule() {
            super(MyComponent.class, new MyModule());
            set(new DaggerMockRule.ComponentSetter<MyComponent>() {
                @Override public void setComponent(MyComponent component) {
                    App app = (App) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
                    app.setComponent(component);
                }
            });
        }
    }
    

    在 Espresso 中使用如下:

    public class MainActivityTest {
    
        @Rule public MyRule daggerRule = new MyRule();
    
        @Rule public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, false, false);
    
        @Mock RestService restService;
    
        @Mock MyPrinter myPrinter;
    
        @Test
        public void testCreateActivity() {
            when(restService.getSomething()).thenReturn("abc");
    
            activityRule.launchActivity(null);
    
            verify(myPrinter).print("ABC");
        }
    }
    

    Dagger Subcomponents

    0.6 版本之后, DaggerMock 开始支持 Subcomponents, 但是有一些限制: subcomponent module 必须作为 subcomponent 在其父 Component 声明时的参数. 例如: 定义一个 subcomponent:

    @Subcomponent(modules = MainActivityModule.class)
    public interface MainActivityComponent {
        void inject(MainActivity mainActivity);
    }
    

    subcomponent 在其父 Component 中声明:

    @Singleton
    @Component(modules = AppModule.class)
    public interface AppComponent {
        // 返回 Subcomponent,  方法的参数为 modules
        MainActivityComponent activityComponent(MainActivityModule module);
    }
    

    Subcomponent 需要在 Dagger 2.1+ 版本才支持, 例子可以参考这里

    DaggerMock 配置

    在项目 build.gradle 中配置 (项目根目录):

    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
    

    在 module 中添加依赖:

    dependencies {
        testCompile 'com.github.fabioCollini:DaggerMock:0.6.2'
        //and/or 如果你需要在Instrumentation、Espresso、UiAutomator里面使用的话
        androidTestCompile 'com.github.fabioCollini:DaggerMock:0.6.2'
    }
    

    相关文章

      网友评论

      • 04189ac4f18d:赞一个 。。很及时
      • 落影loyinglin:赞
        binwin20:@落影loyinglin 哈哈, 这几天在整单元测试, 用到这个. 顺手翻译了, 第一次写简书, 第一次翻译, 好紧张 :grin:

      本文标题:DaggerMock 使用文档

      本文链接:https://www.haomeiwen.com/subject/ytptsttx.html