title: How to test mock interaction on Activity onResume() Using Dagger Modules and Robolectric?
body: <p>I'm using Dagger for my dependency injection, it works well in my app but I have trouble to test it. I've followed this pattern in order to create the modules depenency graph : <a href="https://github.com/pyricau/shipfaster/blob/master/src/main/java/com/squareup/shipfaster/common/ShipFasterApplication.java" rel="nofollow">https://github.com/pyricau/shipfaster/blob/master/src/main/java/com/squareup/shipfaster/common/ShipFasterApplication.java</a></p>
  
  <p>Now, in my MainActivity test class, I want to be able to verify interaction with a mock when the Activity onResume() method is called.</p>
  
  <p>Here is the class :</p>
  
  <pre><code>@Config(emulateSdk = 18)
  @RunWith(RobolectricDaggerTestRunner.class)
  public class MainActivityTest extends TestCase {
  
      @Inject MainActivity sut;
      public @Mock MyObject mockMyObject;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
          ObjectGraph.create(new TestModule()).inject(this);
      }
  
      @Test
      public void testThatMyActivityDelegatesDoSomethingToMyObject(){
          //init
          ActivityController&lt;MainActivity&gt; activityController = ActivityController.of(sut);
  
          //run
          activityController.create().start().resume();
  
          //verify
          Mockito.verify(mockMyObject).doSomething();
      }
  
      @Module(
              includes = {ActivityModule.class},
              injects = MainActivityTest.class,
              overrides = true,
              library = true
      )
      class TestModule {
          @Provides
          MyObject provideMyObject() {
              return mockMyObject;
          }
      }
  }
  </code></pre>
  
  <p>From what I can see, the <code>onCreate()</code> method is called but a real instance of <code>myObject</code> is used, not the mocked one. The test failed with a "wanted but not invoked - Actually, there were zero interactions with this mock." error. </p>
  
  <p>Maybe this is because the MainActivity I'm trying to create using Robolectric is not associated with my TestModule because it's created in the Application level, but I manage to make that test to pass by explicitly call a method on the MainActivity and put the myObject.doSomething() in there, but what I need is to test Android lifecycle calls.</p>
  
  <p>Any idea on how can I manage to test this? </p>