Search code examples
javaandroiddagger-2

DaggerTestApplicationComponent not generated missing binding...Unsure about this


Android Github repo api calling app using Dagger app build but I'm trying to generated a DaggerTestApplicationComponent by running assembleAndroidTest and it breaks on me with the following stacktrace

enter image description here

im tryinng to mirror most of the main classes as test classes but its confusing me reading the stacktrace especially when it builds somewhere I accept I've gone wrong

Please help, advice welcome.

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

}

I have created some test classes mirroring these (this is where my issues are I think....).

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

Any other classes needed can be provided.

GitHub Repo

Thanks


Solution

  • The error basically says you are trying to inject the

    ScreenNavigator into the BaseActivity but Dagger does not know how to get an instance due to a missing provides method or you miss the @Inject annotation for the constructor of ScreenNavigator