fabioCollini / DaggerMock

A JUnit rule to easily override Dagger 2 objects
Apache License 2.0
1.16k stars 96 forks source link

@ContributesAndroidInjector module support #47

Closed p4r4n01d closed 6 years ago

p4r4n01d commented 7 years ago

I am using the dagger2 android injector helper classes and would like to provide mocks of some objects in the ApplicationModule (not shown). However, the module is abstract as dagger generates the activity injector code so I cannot create an instance of the module in my mock TestRule to call the constructor of DaggerMock. Is there a way to mock this without adding an extra subcomponent?

ApplicationModule.java

@Module
@SuppressWarnings("WeakerAccess")
public abstract class ApplicationModule {

    @PerActivity
    @ContributesAndroidInjector(modules = ActivityModule.class)
    abstract BaseActivity contributeBaseActivityInjector();

...
}

Here is my application component, which uses a builder

ApplicationComponent.java

@Singleton
@Component(modules = {ApplicationModule.class, AndroidInjectionModule.class})
public interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(TheApplication app);
        ApplicationComponent build();
    }

Here is the application class.

TheApplication.java

public class TheApplication extends Application implements
        HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> mDispatchingAndroidInjector;

    private ApplicationComponent mApplicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        getComponent().inject(this);
    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return mDispatchingAndroidInjector;
    }

    public ApplicationComponent getComponent() {
        if (mApplicationComponent == null) {
            mApplicationComponent = DaggerApplicationComponent.builder()
                    .application(this)
                    .build();
        }
        return mApplicationComponent;
    }
}
fabioCollini commented 7 years ago

DaggerMock can override only not abstract modules and ContributesAndroidInjector are not supported. But you can try to create a new module (not abstract) with the objects you want to override.

RoRoche commented 7 years ago

Does DaggerMock support the new dagger-android library?

fabioCollini commented 7 years ago

@RoRoche the new dagger-android library is not supported in current version but I am working to support it (at least for replacing objects defined in application component). I hope I'll release a new release soon.

j2emanue commented 7 years ago

@RoRoche could you give me a link to the new dagger library your referring to. Maybe i can help

RoRoche commented 7 years ago

@j2emanue https://google.github.io/dagger/android.html

j2emanue commented 7 years ago

I've been using it with goggles dagger2 . I't seems ok


From: Romain Rochegude notifications@github.com Sent: Thursday, August 31, 2017 10:14:50 PM To: fabioCollini/DaggerMock Cc: j2emanue; Mention Subject: Re: [fabioCollini/DaggerMock] @ContributesAndroidInjector module support (#47)

@j2emanuehttps://github.com/j2emanue https://google.github.io/dagger/android.html

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/fabioCollini/DaggerMock/issues/47#issuecomment-326327549, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AEuvOk6VJGflWO-tgLdXtfkKIwe7tG7_ks5sds3qgaJpZM4N-7cN.

fabioCollini commented 7 years ago

I have pushed a new version to support Dagger Android (you can try it using version 0.7.1-beta1). You must add an invocation to method customizeBuilder to set the application, an example is available here. In this version Dagger Android is supported in Espresso test to replace objects defined in Application component, I'll continue to investigate to find a way to support it in JUnit tests and for subcomponents and dependent components.