Allow fake DispatchingAndroidInjector implementations
See original GitHub issueMy motivation for this is to make it possible to inject different test doubles into Activities on a per-test basis. I’m going to talk about Activities here, but I think Fragments/etc have similar issues which could be resolved the same way.
My particular use case is UI tests - these are more fine-grained than functional/end-to-end integration tests (some people might consider them unit tests). I am familiar with the guidance on testing but the usual suggested approaches all have issues:
- Manual injection isn’t an option because for various reasons - not least of which, if we’re calling
AndroidInjection.inject(this)
at the start ofonCreate()
then it determines what is injected - at least for the duration ofonCreate()
. - Creating separate Component configurations for each test requires creating a complete object graph in each instance. This can involve a lot of boilerplate which quickly results in hard-to-read tests.
Injecting a stub DispatchingAndroidInjector into the Application class would enable injection of the Activity/Activities being tested to be done manually. I can achieve this myself by proxying the call to AndroidInjection.inject(this)
and stubbing the proxy, but it would be nice if this were supported out of the box.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:6
Top GitHub Comments
@liminal I think you’re right, but it’s still a comparatively indirect process that brings with it some boilerplate. Along with the Component you’d need at least one Module.
I feel my suggestion would allow for a reasonable approximation of manual injection into Android classes, with essentially zero impact on production code.
On mobile at the moment, but I’ll try to throw together a gist later to clarify what I mean and compare the two approaches. It’s possible I’m missing something and there’s already a simpler way of doing what I want to achieve.
Awesome, thanks guys. Definitely good to close this once @ronshapiro’s commit gets merged in. And sorry about the mock/fake confusion - I’m a bit mortified since I blogged about that very thing a few months back!