コンポーネントを生成していない私はここにガイドを次しています:ダガー用/テストクラス
public class FlingyApplication extends Application {
@Singleton
@Component(modules = { FlingyModule.class })
public interface FlingyComponent
}
@Module
public class FlingyModule
:
https://github.com/ecgreb/dagger-2-testing-demo
を、私は私のアプリ/ srcに/メイン(射出と@Providesコードは省略)で、次のセットアップを持っていますアプリ/ SRC /試験で
:
public class TestFlingyApplication extends Application {
@Singleton
@Component(modules = { TestFlingyModule.class })
public interface TestFlingyComponent extends FlingyComponent
}
@Module
public class TestFlingyModule
はこれまでのところ、それは、例えば、githubのとほぼ同じです。 daggerがsrc/mainにComponent Builderのコードを生成すると、それらは正しく生成されます。ただし、Daggerはsrc/testのComponent Builderのコードを生成しません。
私の主な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.
私はそれが中間体に構築し、私はapt
の代わりcompile
を使用するようにbuild.gradleファイルを必要とすることを前提とし、なぜ知っているが、私はありませんこれをどのように動作させるかを理解していないようです。私はそれが絶対に可能であることを知っています。
をHTTP ://google.github.io/dagger/testing.html)ユニットテストのために短剣を使用しないようにアドバイスしてください。 –
多くの依存関係がある場合は、pあなたのユニットのテストは1つのユニット以上のテストをテストします。それは、Googleは常にベスト(気分が良い!)とはそれほど広範な声明ではないと述べた。ユニットテストで短剣を使用すると、特定のケースでうまくいくので、この質問にはうってつけです。 – jyanks