title: Replacing Provider injections with Mockito mocks during test with Dagger
body: <p>I am attempting to test-drive a suite of changes I'm making to my Android service (<code>android.app.Service</code>) - I am using Dagger and Robolectric and I need to replace the field injected classes within the service with some mocks to reduce the test scope...make it (slightly) more 'unit' like.</p>
  <h2>So, the short version...</h2>
  <p>I inject <code>Providers.of</code> (Guice syntax there...) into my <code>android.app.Service</code>. How do I replace them with MockProviders during a unit test?</p>
  <h2>The longer version...</h2>
  <p>This is what the relevant service code looks like;</p>
  <pre><code>@Inject SpotService spotService;
  @Inject Provider&lt;SynchroniseTidePosition&gt; syncTidePosition;
  @Inject Provider&lt;SynchroniseSwellDataTask&gt; syncBuoyData;
  @Inject Provider&lt;SynchroniseConditionsTask&gt; syncConditionsData;
  @Inject SpotRatingCalculator spotRatingCalculator;
  @Inject LocalBroadcastManager localBroadcastManager;
  @Inject NotificationManager notificationManager;
  
  /**
   * @see android.app.Service#onCreate()
   */
  @Override
  public void onCreate() {
    super.onCreate();
    inject(this);
  ...
  </code></pre>
  <p>So, under normal operation the <code>startService(intent)</code> call lets the service inject it's dependencies during <code>onCreate</code> and we're all good.</p>
  <p>Under my test I want to replace the injected <code>Provider</code>s <code>get()</code> calls with Mockito mocks. I have attempted to follow the <a href="http://square.github.io/dagger/" rel="noreferrer">Dagger test example</a> and created a test module that looks like this;</p>
  <pre><code>@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);
      }
    }
  </code></pre>
  <p>I am expecting that when my actual Service code calls the Provider <code>get()</code> I would be getting the <em>Mockito</em> mocks back (those are the mocks that my test module @Provides).</p>
  <p>This isn't happening. What's wrong with the approach I'm heading down here?</p>