2017-02-13 85 views
1

我創建了以下測試類。問題是沒有找到DaggerTestDiComponent - 儘管我可以在build目錄中看到它。未找到匕首測試組件

我已經看過類似的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

如果它被編譯,然後defintely類在你的應用程序中...請記住AS #1。關閉最近在手機上的應用程序,然後運行該項目,因爲有時AS只是實現你的最新變化... #2。在setup()中添加一個定時器,在說2秒後調用該代碼,以確認它是因爲加載該類(只是爲了確認) 爲進一步我想更多的代碼或流將幫助(至少對我:)) 忽略,如果它沒有幫助... – Ahmad

+0

我試過#1沒有運氣。由於編譯問題,我無法執行#2。我從我的項目中添加了附加代碼(TestDiComponent和TestAppModule)。 – Eric

回答

3

也許您正在查找androidTest文件夾下的類,並且您沒有將dagger-compile庫作爲androidTestCompileAnnotationProcessor/androidTestCompileAnnotationProcessor添加到您的gradle應用程序文件中。這不允許Dagger編譯器在您的androidTest文件夾下生成DaggerXXX類。

+0

我實際上是在測試(而不是androidTest)域下執行此操作。 – Eric

0

我試着添加對此的評論,但格式很糟糕,所以我將添加爲答案,但它有點不完整。

Android Studio仍然說它無法找到生成的DaggerTestDiComponent類,但是我的代碼執行並且測試運行。

供參考的build.gradle:

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

我也修改了我的TestAppModule.getStationModel不要嘲笑我的StationModel類,因爲我沒能嘲笑它,我以爲我可以的方式(我剛學的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); 
    } 

}