2016-03-26 43 views
41

我导向这里以下:https://github.com/ecgreb/dagger-2-testing-demo匕首不产生组件/测试类

我在我的应用程序/ SRC以下安装/主(喷射和@Provides代码省略):

public class FlingyApplication extends Application { 
    @Singleton 
    @Component(modules = { FlingyModule.class }) 
    public interface FlingyComponent 
} 

@Module 
public class FlingyModule 

在应用程序/ SRC /测试:

public class TestFlingyApplication extends Application { 
    @Singleton 
    @Component(modules = { TestFlingyModule.class }) 
    public interface TestFlingyComponent extends FlingyComponent 
} 

@Module 
public class TestFlingyModule 

到目前为止,它是几乎相同的例子github上。当匕首去为src/main中的组件构建器生成代码时,它们会正确生成。但是,Dagger不会为src/test中的组件构建器生成代码。

我主要的build.gradle:

dependencies { 
    classpath 'com.android.tools.build:gradle:2.1.0-alpha3' 

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.5.1' 
} 

我的应用程序/的build.gradle

apply plugin: 'com.android.application' 
apply plugin: 'com.neenbedankt.android-apt' 


android { 
    # There is obviously more in here, but this is the custom part: 
    packagingOptions { 
     exclude 'META-INF/services/javax.annotation.processing.Processor' 
    } 
} 

dependencies { 
    compile 'com.squareup:otto:1.3.8' 
    compile 'com.android.support:cardview-v7:23.1.1' 
    compile 'com.android.support:recyclerview-v7:23.1.1' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
    compile 'com.jakewharton:butterknife:7.0.1' 

    compile 'com.google.dagger:dagger:2.0.1' 
    apt 'com.google.dagger:dagger-compiler:2.0.1' 
    compile 'javax.annotation:javax.annotation-api:1.2' 

    compile 'io.reactivex:rxandroid:1.1.0' 
    compile 'io.reactivex:rxjava:1.1.0' 

    testCompile 'com.neenbedankt.gradle.plugins:android-apt:1.4' 
    testCompile 'junit:junit:4.12' 
    testCompile 'org.robolectric:robolectric:3.0' 
    testCompile 'org.mockito:mockito-core:1.10.19' 
} 

所以,当我建,我得到的DaggerFlingyApplication_FlingyComponent类,但不是DaggerTestFlingyApplication_TestFlingyComponent

东西我注意到有趣的是,如果我切换线:

apt 'com.google.dagger:dagger-compiler:2.0.1' 
# TO 
compile 'com.google.dagger:dagger-compiler:2.0.1' 

我看到下面当我运行./gradlew compileDebugUnitTestSources

:app:compileDebugJavaWithJavac 
Note: /app/build/generated/source/apt/debug/com/jy/flingy/DaggerFlingyApplication_FlingyComponent.java uses unchecked or unsafe operations. 
Note: Recompile with -Xlint:unchecked for details. 
:app:preDebugUnitTestBuild UP-TO-DATE 
:app:prepareDebugUnitTestDependencies 
:app:compileDebugUnitTestJavaWithJavac 
Note: /app/build/intermediates/classes/test/debug/com/jy/flingy/DaggerTestFlingyApplication_TestFlingyComponent.java uses unchecked or unsafe operations. 

我不知道为什么它建立的中间体和我认为我需要的build.gradle文件中使用apt代替compile,但我似乎无法弄清楚如何让这个工作。我知道这绝对有可能。

+0

本文档(HTTP ://google.github.io/dagger/testing.html)建议不要使用匕首进行单元测试。 –

+0

如果你有很多的依赖关系,那么它可能会您的单元测试可以测试多个单元。这就是说,谷歌并不总是知道最好的(喘气!),这是他们的一个非常广泛的声明。在特殊情况下使用匕首进行单元测试对我们来说效果很好,因此这个问题。 – jyanks

回答

83

您需要添加以下到您的build.gradle文件仪器测试:

androidTestApt 'com.google.dagger:dagger-compiler:<version>' 

或JUnit测试:

testApt 'com.google.dagger:dagger-compiler:<version>' 

这需要产生匕首代码为您的测试组件。


编辑

如果您正在使用jack工具链,然后添加以下 为Android测试:

的JUnit测试:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>' 

编辑

如果您正在使用kotlin-kapt的科特林代码中使用下列内容:

kaptAndroidTest 'com.google.dagger:dagger-compiler:<version>' 

或JUnit测试:

kaptTest 'com.google.dagger:dagger-compiler:<version>' 

检查this链接了解更多信息。

+0

即使测试在/ test中,而不是/ androidTest? – jyanks

+0

您正在编写应该在'androidTest'中的检测测试。如果您正在编写JUnit测试,那么您可以使用'testApt'。 – Abdullah

+4

我得到以下内容:'''错误:(55,0)未找到Gradle DSL方法:'testApt()'''' – jyanks

16

只需添加一点到上面的答案,因为有一些最近的变化。

从Android Gradle插件2.2及以上版本中,您将不再使用testApt

从现在开始,你只需要把这个在的build.gradle

所以:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:<version>' 

但更重要的,我来这里,是这样的:如果你需要gradle这个生成DaggerComponent类为你你将不得不做一些额外的工作。

打开我们的build.gradle文件和android部分后写:

android.applicationVariants.all { variant -> 
    if (variant.buildType.name == "debug") { 
     def aptOutputDir = new File(buildDir, "generated/source/apt/${variant.unitTestVariant.dirName}") 
     variant.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir) 
     assembleDebug.finalizedBy('assembleDebugUnitTest') 
    } 
} 

这将创建目录生成/生成/源/公寓/测试/作为一个Java类收件人和最后一部分将触发“assembleDebugUnitTest”任务,最终在刚刚创建的文件夹中创建这些Dagger2组件。

请注意,此脚本仅针对“调试”变体触发,并使用“assembleDebug”任务利用该构建变体。如果由于某种原因,你需要在其他变体只需稍微调整一下。

为什么Dagger2不会自动做到这一点超出了我,但嘿,我不是亲。

+0

对我来说,你已经错过了结尾'}' –

+0

我喜欢这种方法,因为它不需要更多的Android库来安装。因为我在一个单独的库中进行TDD,所以我开始使用android.libraryVariants.all –

+0

太棒了。在搜索“testApt”的替代方法之后,这样做了。谢谢! – saltandpepper

7

了解Android Studio 3和匕首2.13需要已经提到的注释处理器:

testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.13' 

但也不要忘了androidTest下仪器化检验的做到这一点:

androidTestAnnotationProcessor'com.google.dagger:dagger-compiler:2.13' 

你可能得到这样的印象是行不通的,因为DaggerXYZ类没有生成。几小时后,我发现只有在测试执行时触发测试源代。如果您从Android Studio启动testandroidTest,则应该触发源代。

如果您需要手动这种早期的gradle触发:

gradlew <moduledirectory>:compile<Flavor>DebugAndroidTestSources 
gradlew <moduledirectory>:compile<Flavor>DebugTestSources 

更换Debug如果您在不同的编译型运行测试。

注:

如果您正在使用multiDexEnable =真正的你可能会得到一个错误:

Test running failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'

使用不同的选手在这种情况下:

android { 

    defaultConfig { 
    multiDexEnabled true 
    testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"