As stated in the Dagger Documentation , for unit testing we donβt need to use a dagger at all, and for the example provided, it makes sense
class ThingDoer { private final ThingGetter getter; private final ThingPutter putter; @Inject ThingDoer(ThingGetter getter, ThingPutter putter) { this.getter = getter; this.putter = putter; } String doTheThing(int howManyTimes) { } }
Using this class structure, simply unit test, just mock getter and putter , pass them as constructor arguments, instruct mockito what to return when interacting with any of these objects, and then make statements on doTheThing(...) .
Where I am afraid to test when I should unit test class with a structure like this:
class ThingDoer { @Inject ThingGetter getter; @Inject ThingPutter putter; @Inject ThingMaker maker; @Inject
As you can see, I no longer use constructor injection, but instead injection. The main reason is not that there are too many parameters in the constructor.
Is there a way to inject false dependencies into ThingDoer when all its dependencies are provided via field injection?
source share