2017-02-13 12 views
1

次のテストクラスを作成しました。問題は、DaggerTestDiComponentが見つからないということです。たとえビルドディレクトリで確認できます。ダガーテストコンポーネントが見つかりません

私は同様のSOの質問を見てきましたが、それらは古いバージョンのgradle/Dagger2に関係しているようで、適用できないようです(少なくとも私が見るものから)。私のアプリのDaggerコードは正常に動作しています。

public class TestMvpEngineeringPresenter { 

@Mock 
IMvpEngineeringView iMvpEngineeringView; 

@Inject 
MvpEngineeringPresenter mvpEngineeringPresenter; 

@Rule 
public MockitoRule mockitoRule = MockitoJUnit.rule(); 

@Before 
public void setUp() { 

    TestDiComponent component = DaggerTestDiComponent.builder() 
      .testAppModule(new TestAppModule()).build(); 
    component.inject(this); 
} 

@Test 
public void testStationControlSwitchChange() { 

    mvpEngineeringPresenter.assignEngineeringView(iMvpEngineeringView); 
    mvpEngineeringPresenter.onLoad(); 

    mvpEngineeringPresenter.switchChanged(new SwitchChange(0, true)); 
    assertEquals(true, mvpEngineeringPresenter.iStationModel.getStationControls().get(0).isOnOff()); 
    mvpEngineeringPresenter.switchChanged(new SwitchChange(0, false)); 
    assertEquals(false, mvpEngineeringPresenter.iStationModel.getStationControls().get(0).isOnOff()); 
} 

}

私のbuild.gradleファイルには、次のようになります。

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 25 
buildToolsVersion "25.0.0" 
defaultConfig { 
    applicationId "com.fisincorporated.mvc_mvp_mvvm" 
    minSdkVersion 25 
    targetSdkVersion 25 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

    dataBinding { 
     enabled = true 
    } 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

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


// Android support stuff 
compile 'com.android.support:design:25.0.1' 
compile 'com.android.support:appcompat-v7:25.0.1' 
compile 'com.android.support:recyclerview-v7:25.0.1' 


// Butterknife - also includes library for Dagger 
compile 'com.jakewharton:butterknife:8.4.0' 
compile 'com.google.dagger:dagger:2.8' 
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 

// For MVP Observer/Subscriber 
compile 'io.reactivex:rxandroid:1.2.0' 
compile 'io.reactivex:rxjava:1.1.5' 

// For Dagger2 
// compile 'com.google.dagger:dagger:2.8' // Added above for ButterKnife 
annotationProcessor 'com.google.dagger:dagger-compiler:2.8' 

// For testing 
testCompile 'junit:junit:4.12' 

// Mockito of course! 
testCompile "org.mockito:mockito-core:2.+" 
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.8' 

} 

ここでここでTestDiComponent

​​

だTestAppModule

@Module 
public class TestAppModule { 

@Provides 
public IStationModel getStationModel() { 

    IStationModel iStationModel = Mockito.mock(IStationModel.class); 
    when(iStationModel.getStationName()).thenReturn("Mocked Station"); 
    when(iStationModel.getStationControls().size()).thenReturn(2); 
    when(iStationModel.getBigButtonName()).thenReturn(("Log Button")); 
    when(iStationModel.getLogHint()).thenReturn("Enter log text here"); 

    for (int i = 0; i < 2; ++i) { 
     when(iStationModel.getStationControls().get(i)).thenReturn(new StationControl("Test Switch" + i,false)); 
    } 
    return iStationModel; 
} 

@Provides 
public MvpEngineeringPresenter getMvpEngineeringPresenter() { 
    return new MvpEngineeringPresenter(); 
} 

} 
です
+0

そのコンパイルしたばかりその後、よっぽどクラスはあなたのアプリケーションに存在する場合... #1 AS念頭に。最近の電話から最近のアプリを閉じてから、プロジェクトを実行してください。時にはただ最新の変更を実装するだけなので... #2。セットアップ()でそのコードを呼び出すためのタイマーを追加するだけで、そのクラスをロードすることによってそのコードが確定したことを確認します(確認のため) さらにコードやフローのほうが助かります:) 役に立たなかった場合は無視してください... – Ahmad

+0

私は運が無ければ#1を試しました。私はコンパイルの問題のため#2をすることはできません。私は私のプロジェクトから追加コード(TestDiComponentとTestAppModule)を追加しました。 – Eric

答えて

3

あなたのandroidTestフォルダの下にあるクラスを探しているかもしれません。ダガーコンパイルライブラリをandroidTestCompileAnnotationProcessor/androidTestCompileAnnotationProcessorとしてgradleアプリケーションファイルに追加していないことがあります。これは、daggerコンパイラがandroidTestフォルダの下にDaggerXXXクラスを生成することを許可していません。

+0

私は実際にテスト(notroidTestではなく)の下でこれをやっています。 – Eric

0

私はこれにコメントを追加しようとしましたが、書式設定は面倒だったので、私は答えとして追加しますが、やや不完全です。

Android Studioでは、生成されたDaggerTestDiComponentクラスが見つかりませんでしたが、コードが実行されてテストが実行されます。

参照build.gradleの場合

:私も、私はそれを私は(私は勉強できると思っていた道を模擬することができませんでしたように私StationModelクラスをモックではないし、私のTestAppModule.getStationModelを修正

apply plugin: 'com.android.application' 


android { 
compileSdkVersion 25 
buildToolsVersion "25.0.0" 
defaultConfig { 
    applicationId "com.fisincorporated" 
    minSdkVersion 25 
    targetSdkVersion 25 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

    dataBinding { 
    enabled = true 
    } 
} 
buildTypes { 
    release { 
    minifyEnabled false 
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 

// Android support stuff 
compile 'com.android.support:design:25.0.1' 
compile 'com.android.support:appcompat-v7:25.0.1' 
compile 'com.android.support:recyclerview-v7:25.0.1' 



// Butterknife - also includes library for Dagger 
compile 'com.jakewharton:butterknife:8.4.0' 
compile 'com.google.dagger:dagger:2.9' 
provided 'javax.annotation:jsr250-api:1.0' 
annotationProcessor('com.jakewharton:butterknife-compiler:8.4.0', { 
    exclude group: 'com.android.support', 
    module: 'support-annotations' 
}) 

// For MVP Observer/Subscriber 
compile 'io.reactivex:rxandroid:1.2.0' 
compile 'io.reactivex:rxjava:1.1.5' 

// For Dagger2 
// compile 'com.google.dagger:dagger:2.8' // Added above for ButterKnife 
annotationProcessor 'com.google.dagger:dagger-compiler:2.9' 

// For testing 
testCompile 'junit:junit:4.12' 

// Mockito 
testCompile 'org.mockito:mockito-core:2.4.0' 
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9' 
    //provided 'javax.annotation:jsr250-api:1.0' 

// For Android/Mockito testing 
androidTestCompile 'junit:junit:4.12' 
androidTestCompile('com.android.support.test:runner:0.5', { 
    exclude group: 'com.android.support', 
    module: 'support-annotations' 
}) 
androidTestCompile 'com.android.support.test:rules:0.5' 
androidTestCompile 'org.mockito:mockito-core:2.+' 
androidTestCompile 'com.google.dexmaker:dexmaker:1.2' 
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2' 

// Android espresso testing 
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', 
    module: 'support-annotations' 
    }) 
    // androidTestCompile 'com.android.support.test:runner:0.5' added above 
    // following added to get past version conflict 
androidTestCompile 'com.android.support:support-annotations:25.0.1' 
} 

Mockito)。だから、ここにあるもの:

@Module 
public class TestAppModule { 

    @Provides 
    @Singleton 
    public IStationModel getStationModel() { 

     IStationModel iStationModel = StationModel.getStationModel(); 
     return iStationModel; 
    } 

    @Provides 
    public MvpEngineeringPresenter getMvpEngineeringPresenter(IStationModel istationModel) { 
     return new MvpEngineeringPresenter(istationModel); 
    } 

} 
関連する問題