我创建了以下测试类.问题是找不到DaggerTestDiComponent-即使我在构建目录中也可以看到它.
我已经看过类似的SO问题,但它们似乎与gradle / Dagger2的较早版本有关,似乎不适用(至少从我的观察中可以看出).我的应用程序Dagger代码运行正常.
public class TestMvpEngineeringPresenter {@MockIMvpEngineeringVIEw iMvpEngineeringVIEw;@InjectMvpEngineeringPresenter mvpEngineeringPresenter;@Rulepublic MockitoRule mockitoRule = MockitoJUnit.rule();@Beforepublic voID setUp() { TestDiComponent component = DaggerTestDiComponent.builder() .testAppModule(new TestAppModule()).build(); component.inject(this);}@Testpublic 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 25buildToolsversion "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 stuffcompile '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 Daggercompile 'com.jakewharton:butterknife:8.4.0'compile 'com.Google.dagger:dagger:2.8'annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'// For MVP Observer/Subscribercompile '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 ButterKnifeannotationProcessor 'com.Google.dagger:dagger-compiler:2.8'// For testingtestCompile 'junit:junit:4.12'// Mockito of course!testCompile "org.mockito:mockito-core:2.+"testAnnotationProcessor 'com.Google.dagger:dagger-compiler:2.8'}
这是TestDiComponent
@Singleton@Component(modules = {TestAppModule.class}) // comma separated List of classespublic interface TestDiComponent { voID inject(TestMvpEngineeringPresenter testMvpEngineeringPresenter);}
这是TestAppModule
@Modulepublic class TestAppModule {@ProvIDespublic 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;}@ProvIDespublic MvpEngineeringPresenter getMvpEngineeringPresenter() { return new MvpEngineeringPresenter();}}
解决方法:
也许您正在androIDTest文件夹下找到这些类,并且没有将dagger-compile lib作为androIDTestCompileAnnotationProcessor / androIDTestCompileAnnotationProcessor添加到gradle应用文件中.这不允许dagger编译器在androIDTest文件夹下生成DaggerXXX类.
总结以上是内存溢出为你收集整理的android-找不到Dagger测试组件全部内容,希望文章能够帮你解决android-找不到Dagger测试组件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)