vendredi 22 avril 2016

Change Dagger component before Instrumentation test run

When I run Robotium tests for my application I want that certain dependencies be different from those used in a normal run of the application.

DI happens in the following way: in application class I build the component:

public class App extends Application {

  private static AppComponent component;

  public static AppComponent getComponent() {
      return component;
  }

  public static void setComponent(AppComponent component) {
      App.component = component;
  }

  @Override
  public void onCreate() {
      super.onCreate();

      component = DaggerAppComponent.builder()
              .modelModule(new ModelModule(ModelModule.LaunchType.NORMAL))
              .build();
  }
}

Then the getComponent() method is called from the first activity's onCreate() and gets its dependencies.

From my test class I want to do something like this:

App.setComponent(DaggerAppComponent.builder()
              .modelModule(new ModelModule(ModelModule.LaunchType.TEST))//other type
              .build());

My test is subclassed from ActivityInstrumentationTestCase2 so I tried to do that in setUp() but the method is called after the activity's onCreate() when all dependencies has been already injected.

So my purpose is to replace the component in Robotium tests. When and where can I do that?

Aucun commentaire:

Enregistrer un commentaire