Android Github repo api使用Dagger应用程序构建调用应用程序,但是我试图通过运行assembleAndroidTest来生成DaggerTestApplicationComponent,并且它在我下面的stacktrace中中断了

java - DaggerTestApplicationComponent没有生成缺少的绑定(bind)…对此不确定-LMLPHP

我试图将大多数主要类镜像为测试类,但是它使我无法阅读堆栈跟踪,尤其是当它在某个地方构建时,我接受了我错了

请帮助,欢迎咨询。

ApplicationComponent -------------------------------

@Singleton
@Component(modules = {
        ApplicationModule.class,
        ActivityBindingModule.class,
        ServiceModule.class,
        RepoServiceModule.class,


})
public interface ApplicationComponent {
    void inject(MyApplication myApplication);
}


ApplicationModule ------------------------------

@Module
public class ApplicationModule {

    private final Application application;

    ApplicationModule(Application application) {

        this.application = application;
    }

    @Provides
    Context provideApplicationContext() {
        return application;
    }
}


ActivityBindingModule -----------------------------

@Module(subcomponents = {
        MainActivityComponent.class
})
public abstract class ActivityBindingModule {

    @Binds
    @IntoMap
    @ActivityKey(MainActivity.class)
    abstract AndroidInjector.Factory<? extends Activity> provideMainActivityInjector (MainActivityComponent.Builder  builder);



}


ServiceModule -------------------------------------

@Module(includes = NetworkModule.class)
public abstract class ServiceModule {

    @Provides
    @Singleton
    static Moshi provideMoshi() {
        return new Moshi.Builder()
                .add(AdapterFactory.create())
                .add(new ZoneDateTimeAdapter())
                .build();
    }

    @Provides
    @Singleton
    static Retrofit provideRetrofit(Moshi moshi, Call.Factory callFactory, @Named("base_url") String baseUrl) {
        return new Retrofit.Builder()
                .callFactory(callFactory)
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();

    }
}


RepoServiceModule -----------------------------

@Module
public abstract class RepoServiceModule {


   @Provides
   @Singleton
   static RepoService provideRepoService(Retrofit retrofit) {
       return retrofit.create(RepoService.class);
   }

}


我已经创建了一些反映这些测试类的测试类(这是我认为的问题所在。)。

TestApplicationComponent -------------------------

@Singleton
@Component(modules = {
        ApplicationModule.class,
        TestActivityBindingModule.class,
        TestRepoServiceModule.class,
        ServiceModule.class,
})
public interface TestApplicationComponent extends ApplicationComponent {

}


TestActivityBindingModule -------------------------

@Module(subcomponents = {
        TestMainActivityComponent.class,
})
public abstract class TestActivityBindingModule {


    @Binds
    @IntoMap
    @ActivityKey(MainActivity.class)
    abstract AndroidInjector.Factory<? extends Activity> bindMainActivityInjector(TestMainActivityComponent.Builder builder);
}


TestRepoServiceModule ---------------------------

@Module
public abstract class TestRepoServiceModule {

    @Binds
    abstract RepoService bindRepoService(TestRepoService repoService);
}


可以提供任何其他需要的类。

GitHub Repo

谢谢

最佳答案

该错误基本上表明您正在尝试注入

ScreenNavigator进入BaseActivity,但是Dagger不知道如何获取实例,因为它缺少提供方法,或者您错过了ScreenNavigator构造函数的@Inject批注

09-30 15:14