Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

It is impossible to test @AfterInject methods in Fragments/Views (call for simple dependency injection) #918

Description

@cable729

I am almost certain that there is no way to test my @AfterInject methods. Take a look at the generated init_ method:

private void init_(Bundle savedInstanceState) {
    OnViewChangedNotifier.registerOnViewChangedListener(this);
    imageController = ImageController_.getInstance_(getActivity());
    myAfterInjectActivity();
}

I can't mock before onCreate() because the dependency will be overwritten. I can't mock after because the method I'm testing already ran. I can't mock out ImageController_.getInstance_() either, because the generated ImageController_ is final.


Ideally, I'd like to be able to use a dependency injection strategy, something like a module where I can setup:

// This would be the default. Existing users wouldn't need to create a module
binder.bind(ImageController.class).toGeneratedClass();

// In my testing method I could bind it to something else:
ImageController mock = mock(ImageController.class);
when(mock.downloadGiantVideoAndReturnUri).thenReturn("fake uri");
binder.bind(ImageController.class).to(mock);

Also useful would be binder.bindOnce, so I could do this individually in each method. If we did this kind of setup robotium users could do the following:

MyActivity activity = Robolectric.buildActivity(MyActivity_.class)
                                                 .create()
                                                 .get();

To implement this, we would need to replace generated code like this:

imageController = ImageController_.getInstance_(getActivity());

with

imageController = binder.getInstance(ImageController_.class, getActivity());

Any thoughts would be lovely. I'm still mulling over this, but hopefully I'm going in the right direction.


Additional thoughts: EBean injections should be handled the same way, so we can test EBeans that rely on other EBeans.

Activity

  1. WonderCsabo commented on Apr 7, 2014

    @WonderCsabo
    Member

    I agree with you. BTW, i think there is another issue for custom Bean binding.

  2. WonderCsabo commented on Aug 22, 2014

    @WonderCsabo
    Member

    BTW, you can mock ImageController_.getInstance_() with PowerMock.

  3. WonderCsabo commented on Mar 25, 2015

    @WonderCsabo
    Member

    Closing in favor of #989.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions