dagger-2

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

大憨熊 提交于 2019-11-27 06:20:35
I have a simple Android activity with a single dependency. I inject the dependency into the activity's onCreate like this: Dagger_HelloComponent.builder() .helloModule(new HelloModule(this)) .build() .initialize(this); In my ActivityUnitTestCase I want to override the dependency with a Mockito mock. I assume I need to use a test-specific module which provides the mock, but I can't figure out how to add this module to the object graph. In Dagger 1.x this is apparently done with something like this : @Before public void setUp() { ObjectGraph.create(new TestModule()).inject(this); } What's the

Dagger2 scopes and activity lifecycle

…衆ロ難τιáo~ 提交于 2019-11-27 06:15:56
问题 I have an Android Activity that I'm using Dagger2 to inject a Presenter into. I'd like my Presenter to be capable of holding state even if a configuration change occurs. For instance, I'm going to use the Presenter to kick off a network call and if the user rotates the device while the network call is in-flight I'd like to be able to receive the response after the device finishes its rotation and not have to restart the call. I'm getting tripped up because if I scope the instance of Presenter

Scopes in Dagger 2

拜拜、爱过 提交于 2019-11-27 05:36:13
问题 I probably missed something, but I thought Scopes like @Singleton are used to define "scoped lifecycles". I use Dagger 2 in an Android app (but I don't think the problem is android related at all). I have 1 Module: @Module public class MailModule { @Singleton @Provides public AccountManager providesAccountManager() { return new AccountManager(); } @Singleton @Provides public MailProvider providesMailProvider(AccountManager accountManager) { return new MailProvider(accountManager); } } I have

Dagger 2 injecting parameters of constructor

左心房为你撑大大i 提交于 2019-11-27 05:17:15
问题 I saw the following example on the Dagger 2 website: class Thermosiphon implements Pump { private final Heater heater; @Inject Thermosiphon(Heater heater) { this.heater = heater; } ... } and the documentation: When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor. When I write a Module to provide a Thermosiphon like @Module public class ThermosiphonModule { @Provides @Singleton Thermosiphon provideThermosiphon() { return new

Dagger 2: Injecting user inputted parameter into object

假装没事ソ 提交于 2019-11-27 05:14:34
问题 Say I have a class Util that takes in a object - an instance of class Validator . Since I want to avoid instantiating the Validator class within Util, I pass it in via a constructor: public class Util { @Inject public Util(Validator validator) { } } I have a module that provides the Validator instance: @Provides @Singleton Validator provideValidator() { return Validator.getInstance(); } and an instance of the Util class: @Provides Util provideUtil(Validator validator) { return new Util

Dagger2 and Android

安稳与你 提交于 2019-11-27 03:34:45
问题 I am trying to implement Dagger Dependency Injection into my app but I am having a hard time understanding how it works, especially coming from Spring where DI was much easier and much more declarative. What I want to do is have a bunch of inject-ready objects that can be used throughout my app, that is the SharedPreferences, the Network objects (OkHttp, Retrofit, Picasso...), and EventBus and a SchedulerProvider object for RxJava. This sample seems to offer everything I need but I am having

Dagger 2 generated test component not recognized

只愿长相守 提交于 2019-11-27 03:21:36
问题 I'm hoping that this is just something I'm doing wrong here. I'm trying to use Dagger 2.0 to inject dependencies for my JUnit tests ( not Espresso tests , just pure JUnit). So, I have a 'main' java module and a 'test' java module. In the main module, I've got a Dagger Module and a Component: @Module public class MainModule { @Provides public Widget provideWidget() { return new ConcreteWidget(); } } ... @Component (modules = MainModule.class) public interface MainComponent { void inject

Dagger 2 multibindings with Kotlin

≯℡__Kan透↙ 提交于 2019-11-27 02:30:59
问题 I have the following snippet in my dagger 2 module @Singleton @Provides @ElementsIntoSet fun providesQueries(foo: Foo): Set<Foo>{ val queries = LinkedHashSet<Foo>() queries.add(foo) return queries } I try to inject into in this way @Inject lateinit var foo: Set<Foo> But dagger shows an error which says that Dagger cannot provides java.util.Set without @Provides or @Produces method. I did the same in java and it worked. Does somebody know why is it failing? 回答1: As it described in the Kotlin

What determines the lifecycle of a component (object graph) in Dagger 2?

。_饼干妹妹 提交于 2019-11-27 02:30:50
I'm trying to wrap my head around scopes in Dagger 2, specifically the lifecycle of scoped graphs. How do you create a component that will be cleaned up when you leave the scope. In the case of an Android application, using Dagger 1.x you generally have a root scope at the application level which you'd extend to create a child scope at the activity level. public class MyActivity { private ObjectGraph mGraph; public void onCreate() { mGraph = ((MyApp) getApplicationContext()) .getObjectGraph() .plus(new ActivityModule()) .inject(this); } public void onDestroy() { mGraph = null; } } The child

Dagger 2 on Android @Singleton annotated class not being injected

假如想象 提交于 2019-11-27 02:22:50
问题 I am currently trying to integrate Dagger 2 into an Android application. My project setup is as follows: library app (depends on library) In my library project I defined a class that I'll later inject into other classes that need it (Activities and regular classes) in the library as well as the app project. @Singleton public class MyManager{ @Inject public MyManager(){ //Do some initializing } } Now - for instance in my Fragments or Activities or regular classes I'd inject the above Singleton