我创建了以下测试类。问题是没有找到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();
}
}
如果它被编译,然后defintely类在你的应用程序中...请记住AS #1。关闭最近在手机上的应用程序,然后运行该项目,因为有时AS只是实现你的最新变化... #2。在setup()中添加一个定时器,在说2秒后调用该代码,以确认它是因为加载该类(只是为了确认) 为进一步我想更多的代码或流将帮助(至少对我:)) 忽略,如果它没有帮助... – Ahmad
我试过#1没有运气。由于编译问题,我无法执行#2。我从我的项目中添加了附加代码(TestDiComponent和TestAppModule)。 – Eric