title: Mock field in Android Activity in Robolectric test
body: <p>So, I'm working in legacy code, and I want to unit test one of the easier screens, namely the login. 
  I don't have any DI framework.  </p>
  
  <p>I use robolectric 4.0.1 and mockito 1.10.19</p>
  
  <p>Currently, my test fails due to a NullPointer on an object that I tried to mock:</p>
  
  <p>TestClass</p>
  
  <pre><code>@RunWith(RobolectricTestRunner.class)
  public class LoginActivityTest {
      @Rule
      public MockitoRule mockitoRule = MockitoJUnit.rule();
  
      @Mock
      FirebaseInstanceId firebaseInstanceId;
  
      @InjectMocks
      private LoginActivity activity;
  
      @Before
      public void setup() {
  
          when(firebaseInstanceId.getToken()).thenReturn("mockToken");
          ActivityController&lt;LoginActivity&gt; activityController = Robolectric.buildActivity(LoginActivity.class);
          activity = activityController.get();
          initMocks(this);
          activityController.create();
      }
  
      @Test
      public void checkThatLoginButtonExists() {
          // does not reach this point
          Button btn = (Button) activity.findViewById(R.id.button_login);
  
          assertNotNull("Button exisits", btn);
      }
  }
  </code></pre>
  
  <p>class under test</p>
  
  <pre><code>public class LoginActivity extends FragmentActivity {
      private Button mBtnLogin;
  
      public LoginActivity() {
          //default constructor
      }
  
      private FirebaseInstanceId firebaseInstanceId;
      private FirebaseInstanceId getFirebaseInstanceId(){
          if (firebaseInstanceId == null){
              firebaseInstanceId = FirebaseInstanceId.getInstance();
          }
          return firebaseInstanceId;
      }
  
      private void initViews() {
          ...
          mBtnLogin = (Button) findViewById(R.id.button_login);
          ...
      }    
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.login_layout);
          ...
          initViews();
          ...
          registerNotification();
      }
  
      private void registerNotification() {
          String newFcmToken = getFirebaseInstanceId().getToken(); // fails here
          ...
      }
  }
  </code></pre>
  
  <p>I've tried mainly to reorder the instructions in my TestClass, based on google findings, but nothing seems to work.
  As far as I understand it, firebaseInstanceId should not be null when the <code>getFireBaseInstanceId</code> method is called since it is injected with initMocks(this) during the <code>setup()</code> method.</p>
  
  <p>This works for other tests, but none of those actually combine robolectric and mockito.</p>
  
  <p>EDIT1:<br>
  I've put the initMocks(this) as the first line in setup()<br>
  I've added some System.out.println to see what happens, and I've noticed that the LoginActivity constructor gets calles multiple times(once for initMocks, once for Robolectric.buildActivity) </p>
  
  <p>EDIT2:
  I've changed my setup in my test class to be as follows</p>
  
  <pre><code>private void setMyOwnMock(String fieldName, Object inClass, Object mock ){
          Field declaredField;
          try {
              declaredField = inClass.getClass().getDeclaredField(fieldName);
              declaredField.setAccessible(true);
              declaredField.set(inClass, firebaseInstanceId);
          } catch (NoSuchFieldException e) {
              e.printStackTrace();
          } catch (IllegalAccessException e) {
              e.printStackTrace();
          }
      }
  
      @Before
      public void setup() {
          ActivityController&lt;LoginActivity&gt; activityController = Robolectric.buildActivity(LoginActivity.class);
          activity = activityController.get();
  
          setMyOwnMock("firebaseInstanceId", activity, firebaseInstanceId);
  
          when(firebaseInstanceId.getToken()).thenReturn("mockToken");
  
  //        System.out.println(activity.firebaseInstanceId == null);
  
          activityController.create();
      }
  </code></pre>
  
  <p>while maybe not as clean as I would like, it gets the job done without having to change my production code.</p>