简体   繁体   English

未找到Dagger测试组件

[英]Dagger test component not found

I have created the following test class. 我创建了以下测试类。 The problem is that DaggerTestDiComponent is not being found - even though I can see it in the build directory. 问题是找不到DaggerTestDiComponent - 即使我可以在构建目录中看到它。

I have looked through similar SO questions but they seem to concern older versions of gradle/Dagger2 and don't seem to apply (at least from what I can see). 我看过类似的SO问题,但他们似乎关注旧版本的gradle / Dagger2并且似乎不适用(至少从我能看到的)。 My app Dagger code is working OK. 我的应用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());
}

} }

My build.gradle file looks like this: 我的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'

}

Here's TestDiComponent 这是TestDiComponent

@Singleton
@Component(modules = {TestAppModule.class})  // comma separated list of  classes
public interface TestDiComponent {

    void inject(TestMvpEngineeringPresenter testMvpEngineeringPresenter);

}

Here is TestAppModule 这是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();
}

}

Maybe you are locating the classes under your androidTest folder and you are not adding dagger-compile lib as androidTestCompileAnnotationProcessor / androidTestCompileAnnotationProcessor to your gradle app file. 也许您正在androidTest文件夹下找到类,并且您没有将dagger-compile lib作为androidTestCompileAnnotationProcessor / androidTestCompileAnnotationProcessor到您的gradle应用程序文件中。 That is not allowing dagger compiler to generate you DaggerXXX classes under your androidTest folder. 这不允许dagger编译器在你的androidTest文件夹下生成DaggerXXX类。

I tried to add comment on this but formatting was lousy so I will add as answer but it is somewhat incomplete. 我试图添加评论,但格式很糟糕所以我会添加作为答案,但它有点不完整。

Android Studio still says it can't find the generated DaggerTestDiComponent class, but my code does execute and the test runs. Android Studio仍然说它无法找到生成的DaggerTestDiComponent类,但我的代码确实执行并且测试运行。

For reference build.gradle: 供参考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'
}

I also modified my TestAppModule.getStationModel to not mock my StationModel class as I wasn't able to mock it the way I thought I could (I am just learning Mockito). 我还修改了我的TestAppModule.getStationModel以不模拟我的StationModel类,因为我无法以我认为的方式模拟它(我只是在学习Mockito)。 So here that is: 所以这就是:

@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);
    }

}

I just added 我刚才补充道

 testAnnotationProcessor 'com.google.dagger:dagger-compiler:${daggerVersion}'

and it works for me 它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM