question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow fake DispatchingAndroidInjector implementations

See original GitHub issue

My 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 of onCreate() then it determines what is injected - at least for the duration of onCreate().
  • 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:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

1reaction
vaughandroidcommented, Mar 22, 2017

@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.

0reactions
vaughandroidcommented, Mar 25, 2017

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set up HasDispatchingFragmentInjector in dagger ...
I have a Fragment invoking AndroidSupportInjection.inject in its onAttach method. Looking at the implementation of that method, it seems like I ...
Read more >
New Android Injector with Dagger 2 — part 1 | by Mert SIMSEK
Both activities inject to their presenter implementations and make api call(not actually http call, I created a fake method.).
Read more >
Activity Espresso Test With Dagger's Android Injector
We will inject the Presenter to the Activity so in the test, we will inject mock of the Presenter instead of real implementation...
Read more >
Using Dagger 2 for dependency injection in Android - Tutorial
In our simple app, there is an activity that allows a user to authenticate his credentials. The result value of the implementation is...
Read more >
Maintainable Architecture – Dependency Injection
While this removes the dependency on the concrete implementations from our ... instance which means that we can use a mock in these...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found