Dagger 2生成的测试组件无法识别 [英] Dagger 2 generated test component not recognized

查看:130
本文介绍了Dagger 2生成的测试组件无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这只是我在这里做错了。我正在尝试使用Dagger 2.0为我的JUnit测试注入依赖项(不是Espresso测试,只是纯JUnit)。所以,我有一个'主'java模块和'测试'java模块。在主模块中,我有一个Dagger模块和一个组件:

I'm hoping that this is just something I'm doing wrong here. I'm trying to use Dagger 2.0 to inject dependencies for my JUnit tests (not Espresso tests, just pure JUnit). So, I have a 'main' java module and a 'test' java module. In the main module, I've got a Dagger Module and a Component:

@Module
public class MainModule {
    @Provides
    public Widget provideWidget() {
        return new ConcreteWidget();
    }
}

...

@Component (modules = MainModule.class)
public interface MainComponent {
    void inject(WidgetConsumer consumer);
}

在我的测试模块中,我有以下内容:

And in my test module, I have the following:

@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的依赖关系如下所示:

My build.gradle has dependencies that look like this:

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'
}

无论出于何种原因,Dagger生成 DaggerMainComponent ,但拒绝生成 DaggerTestComponent 。我构建时,gradle输出中似乎没有错误。

For whatever reason, Dagger generates DaggerMainComponent, but refuses to generate DaggerTestComponent. There appear to be no errors in the gradle output when I build.

这就是......我认为注释处理器正在运行,但不知何故,android gradle插件无法在编译时拉入这些生成的源。我已经检查了app / build / generated / source / apt / test /目录,并在那里找到了 DaggerTestComponent.java ,但出于某种原因,它并未作为依赖项导入。

Here's the thing... I think the annotation processor is running, but somehow the android gradle plugin is failing to pull in those generated sources during compile-time. I have inspected the app/build/generated/source/apt/test/ directory and found DaggerTestComponent.java in there, but for some reason, it's not imported as a dependency.

有什么想法吗? 以下是显示我的问题的测试项目的链接

推荐答案

android之后将其添加到 build.gradle DSL:

Add this to build.gradle after android DSL:

android {
    ...
}

android.applicationVariants.all {
    def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
    it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}

此后,您的测试组件将被识别。

Thereafter, your test component would be recognized.

对于Kotlin,使用生成/ source / kapt /来替换生成/ source / apt /...

For Kotlin replace generated/source/apt/... with generated/source/kapt/...

跟踪器中提出的一个问题

这篇关于Dagger 2生成的测试组件无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆