我导向这里以下: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
,但我似乎无法弄清楚如何让这个工作。我知道这绝对有可能。
本文档(HTTP ://google.github.io/dagger/testing.html)建议不要使用匕首进行单元测试。 –
如果你有很多的依赖关系,那么它可能会您的单元测试可以测试多个单元。这就是说,谷歌并不总是知道最好的(喘气!),这是他们的一个非常广泛的声明。在特殊情况下使用匕首进行单元测试对我们来说效果很好,因此这个问题。 – jyanks