How do you override a module/dependency in a unit test with Dagger 2.0?

后端 未结 8 2463
一个人的身影
一个人的身影 2020-11-30 19:13

I have a simple Android activity with a single dependency. I inject the dependency into the activity\'s onCreate like this:

Dagger_HelloComponen         


        
8条回答
  •  佛祖请我去吃肉
    2020-11-30 20:03

    With Dagger2, you can pass a specific module (the TestModule there) to a component using the generated builder api.

    ApplicationComponent appComponent = Dagger_ApplicationComponent.builder()
                    .helloModule(new TestModule())
                    .build();
    

    Please note that, Dagger_ApplicationComponent is a generated class with the new @Component annotation.

提交回复
热议问题