Overriding Binding in Guice

前端 未结 5 834
我在风中等你
我在风中等你 2020-11-28 03:37

I\'ve just started playing with Guice, and a use-case I can think of is that in a test I just want to override a single binding. I think I\'d like to use the rest of the pr

5条回答
  •  [愿得一人]
    2020-11-28 04:13

    In a different setup, we have more than one activity defined in separate modules. The activity that's being injected into is in an Android Library Module, with its own RoboGuice module definition in the AndroidManifest.xml file.

    The setup looks like this. In the Library Module there are these definitions:

    AndroidManifest.xml:

    
        
    
    

    Then we have a type being injected:

    interface Foo { }
    

    Some default implementation of Foo:

    class FooThing implements Foo { }
    

    MainModule configures the FooThing implementation for Foo:

    public class MainModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(Foo.class).to(FooThing.class);
        }
    }
    

    And finally, an Activity that consumes Foo:

    public class SomeActivity extends RoboActivity {
        @Inject
        private Foo foo;
    }
    

    In the consuming Android Application Module, we would like to use SomeActivity but, for testing purposes, inject our own Foo.

    public class SomeOtherActivity extends Activity {
        @Override
        protected void onResume() {
            super.onResume();
    
            Intent intent = new Intent(this, SomeActivity.class);
            startActivity(intent);
        }
    }
    

    One might argue to expose the module handling to the client application, however, we need to mostly hide the components being injected because the Library Module is an SDK, and exposing pieces has larger implications.

    (Remember, this is for testing, so we know the internals of SomeActivity, and know it consumes a (package visible) Foo).

    The way I found that works makes sense; use the the suggested override for testing:

    public class SomeOtherActivity extends Activity {
        private class OverrideModule
                extends AbstractModule {
    
            @Override
            protected void configure() {
                bind(Foo.class).to(OtherFooThing.class);
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            RoboGuice.overrideApplicationInjector(
                    getApplication(),
                    RoboGuice.newDefaultRoboModule(getApplication()),
                    Modules
                            .override(new MainModule())
                            .with(new OverrideModule()));
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            Intent intent = new Intent(this, SomeActivity.class);
            startActivity(intent);
        }
    }
    

    Now, when SomeActivity is started, it will get OtherFooThing for its injected Foo instance.

    It's a very specific situation where, in our case, OtherFooThing was used internally to record test situations, while FooThing was used, by default, for all other uses.

    Keep in mind, we are using #newDefaultRoboModule in our unit tests, and it works flawlessly.

提交回复
热议问题