我希望这只是我在这里做错了。我试图用Dagger 2.0为我的JUnit测试注入依赖项(而不是Espresso测试,只是纯粹的JUnit)。所以,我有一个'main'java模块和一个'test'java模块。在主模块,我有一把匕首模块和组件:匕首2生成的测试组件不被识别
@Module
public class MainModule {
@Provides
public Widget provideWidget() {
return new ConcreteWidget();
}
}
...
@Component (modules = MainModule.class)
public interface MainComponent {
void inject(WidgetConsumer consumer);
}
而且在我的测试模块,我有以下几点:
@Module
public class TestModule {
@Provides public Widget provideWidget() {
return new Widget() {
@Override
public void doThing() {
int y = 6;
y ++;
}
};
}
}
...
@Component(modules = TestModule.class)
public interface TestComponent extends MainComponent{
}
我的build.gradle具有依赖性,看起来像这样的:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.2.0'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger:2.9'
testCompile 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9'
}
无论出于何种原因,匕首产生DaggerMainComponent
,但拒绝产生DaggerTestComponent
。当我建立时,似乎在gradle输出中没有错误。
这是事情......我认为注释处理器正在运行,但不知何故,android gradle插件无法在编译期间拉入那些生成的源代码。我检查了app/build/generated/source/apt/test /目录,发现里面有DaggerTestComponent.java
,但由于某种原因,它没有作为依赖项导入。
有什么想法? Here is a link to a test project showing my issue
如果可能,请发布测试项目。 – azizbekian
当然可以。编辑我的回应,添加一个链接到测试项目。 – Alex