How do you define dagger components and modules in tests?

断了今生、忘了曾经 提交于 2019-12-08 11:53:30

问题


I'm trying to create dagger 2 components and/or modules in test classes. This is to create (test) components to test individual production modules without having to have those superfluous components in the main build path. I find that Dagger 2 doesn't detect declarations under test. How do you configure that in gradle? See the example below (which doesn't reference anything in main) - (btw setting testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.24' doesn't help).

@RunWith(MockitoJUnitRunner.class)
public class WidgetTest {

    @Mock
    Widget widget;

    @Component(modules= MockedWidgetModule.class)
    interface MyWidgetFactory {
        Widget getWidget();
    }

    @Module
    class MockedWidgetModule {
        @Provides
        Widget getWidget() {
            return widget;
        }
    }

    @Test
    public void widget_useMockWidget() {
        MyWidgetFactory factory = DaggerMyWidgetFactory.create();
        Widget widget = factory.getWidget();
        widget.doWork();
    }
}

来源:https://stackoverflow.com/questions/57759253/how-do-you-define-dagger-components-and-modules-in-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!