@Override
  public synchronized void initialize(Context context) {
    // Delete the default (renamed) shared preferences to avoid passing state between tests
    PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit();

    super.initialize(context);

    // Ignore requests to launch services from TestableActivity instances during tests.
    // This does not break the good-enough behavior needed in most tests and at the same time
    // does not introduce spurious actions due to services starting during tests.
    StartServiceListener startServiceListener = mock(StartServiceListener.class);
    doReturn(true)
        .when(startServiceListener)
        .onStartServiceInvoked(Mockito.anyObject(), Mockito.anyObject());
    setStartServiceListener(startServiceListener);
  }
Dependency injector module used during automated testing. The module aims to leave as much
functionality in the app as possible, while also stubbing/resetting certain aspects to avoid
inter-test state leakage, spurious actions in response to events outside of the app, and
dependency on the configuration of the test device.
javatests/com/google/android/apps/authenticator/testability/DependencyInjectorModuleForIntegrationTesting.java