android - Replacing Provider injections with Mockito mocks during test with Dagger -
i attempting test-drive suite of changes i'm making android service (android.app.service
) - using dagger , robolectric , need replace field injected classes within service mocks reduce test scope...make (slightly) more 'unit' like.
so, short version...
i inject providers.of
(guice syntax there...) android.app.service
. how replace them mockproviders during unit test?
the longer version...
this relevant service code looks like;
@inject spotservice spotservice; @inject provider<synchronisetideposition> synctideposition; @inject provider<synchroniseswelldatatask> syncbuoydata; @inject provider<synchroniseconditionstask> syncconditionsdata; @inject spotratingcalculator spotratingcalculator; @inject localbroadcastmanager localbroadcastmanager; @inject notificationmanager notificationmanager; /** * @see android.app.service#oncreate() */ @override public void oncreate() { super.oncreate(); inject(this); ...
so, under normal operation startservice(intent)
call lets service inject it's dependencies during oncreate
, we're good.
under test want replace injected provider
s get()
calls mockito mocks. have attempted follow dagger test example , created test module looks this;
@module(includes = oceanlifemodule.class, injects = {testsynchronisationservicenotifications.class}, overrides = true) static class testsynchronisationservicenotificationsmodule { @provides localbroadcastmanager providelocalbroadcastmanager() { return localbroadcastmanager.getinstance(robolectric.application); } @provides notificationmanager providesnotificationmanager() { return (notificationmanager) robolectric.application.getsystemservice(context.notification_service); } @provides spotservice providespotservice() { return mock(spotservice.class); } @provides spotratingcalculator providespotratingcalculator() { return mock(spotratingcalculator.class); } @provides synchronisetideposition providesynctideposition() { return mock(synchronisetideposition.class); } @provides synchroniseconditionstask providesyncconditionstask() { return mock(synchroniseconditionstask.class); } @provides synchroniseswelldatatask providesyncswelldatatask() { return mock(synchroniseswelldatatask.class); } }
i expecting when actual service code calls provider get()
getting mockito mocks (those mocks test module @provides).
this isn't happening. what's wrong approach i'm heading down here?
make own providers.of()
:
public static <t> provider<t> of(final t t) { return new provider<t>() { public t get() { return t; } } }
dagger should include in testing module.
Comments
Post a Comment