Closed jrstarke closed 10 years ago
Hi Jamie, welcome to the wonderful world of Dagger! Can you do us a favor and move this question to StackOverflow with the 'dagger' tag? That way it can be answered by the community and live on in a more searchable form (to hopefully help others).
Thanks!
Thanks @JakeWharton, It's now up at http://stackoverflow.com/questions/23177280/how-to-design-an-android-application-using-dagger-with-testing-in-mind
I'm new to dagger, and I recently started using dagger in one of my own projects, because the concept of being able to handle the dependency injection differently for testing and production, thus being able to inject mock objects that I could use for testing was great.
I modified my application to follow the style laid out in the dagger simple-android example.
After setting it all up, I found that there was problems with injection, and I couldn't fully overload the injections from my production application with the testing logic.
I'm looking for advice on how to set this up in a way that my tests can actually inject differentially with mocks or other objects for testing as needed, and not be too kludgy. Currently, the MainActivityTest is injected correctly, but when we get to the MainActivity, it goes to the PhoneApplication and injects using it's object graph
I've included what I have below. Any help would be greatly appreciated!
Here is my PhoneApplication, based on the DemoApplication.
And here's my AndroidModule
Based on the example, I also set up my Activities to use a base Activity.
Then within my MainActivity I have the following
This worked great and the injections slid into place really quickly, and I was ecstatic. Until I actually wanted to do some testing. The first test I wanted to perform was on my MainActivity.
in the
onCreate
method above, we inject with the LocalBroadcastManager from AndroidModule, instead of the one from MainActivityTest, because we don't currently have a way of telling the PhoneApplication or the Activities that they should use a different object graph.This is a really simple test to get me started. I know that in the onCreate, we're going to register a BroadcastReceiver, so lets just make sure it registered. Because the test has the mockLocalBroadcastManager, but the activity uses the production LocalBroadcastManager, the verify fails.