title: Override Dagger Module in Robolectric Tests
body: <p>I'm working on an <code>Android</code> application and I've been using <code>Dagger</code> for dependency injection.</p>
  
  <p>I'm trying to now test a fragment which has one of these dependencies, let's call it <code>ProductsService</code>.</p>
  
  <p>In my <code>Robolectric</code> test I have got as far as having a test module that overrides <code>ProductsService</code>:</p>
  
  <pre><code>    @Module(
          includes = ProductsModule.class,
          injects = {
                  Fragment.class,
                  FragmentTest.class
          },
          overrides = true
  )
  static class MockProductsModule {
      @Provides
      @Singleton
      public ProductsService providesProductsService() {
          return Mockito.mock(ProductsService.class);
      }
  }
  </code></pre>
  
  <p>In my test, in order to run my fragment I construct it as follows (as seen here <a href="https://stackoverflow.com/questions/11333354/how-can-i-test-fragments-with-robolectric">How can I test fragments with Robolectric?</a>)</p>
  
  <pre><code>        FragmentActivity activity = Robolectric.buildActivity(FragmentActivity.class)
              .create()
              .start()
              .resume()
              .get();
  
      FragmentManager fragmentManager = activity.getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(fragment, null);
      fragmentTransaction.commit();
  </code></pre>
  
  <p>The problem is, during this creation it makes call to <code>Dagger</code> to satisfy it's dependencies:</p>
  
  <pre><code>((MyApplication)getActivity().getApplication()).inject(this);
  </code></pre>
  
  <p>How do I override the object graph created when the fragment is created, to use the <code>MockProductsModule</code> I declare in my test?</p>