问题
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