Overriding debug module in tests

无人久伴 提交于 2019-12-22 09:57:02

问题


I have a Gradle app with a project structure similar to Jake Wharton's u2020:

/src
    /androidTest
    /debug
    /main
    /release

In my application class I build the Dagger graph and inject it:

MyApplication extends Application {
      ...
      public void buildObjectGraphAndInject() {
          Object[] modules = Modules.list(this);
          mApplicationGraph = ObjectGraph.create(modules);
          mApplicationGraph.inject(this);
      }
      ...
}

Inside the debug sourceset, I define Modules.list() as:

public final class Modules {
  public static Object[] list(MyApplication app) {
    return new Object[] {
        new AppModule(app),
        new DebugAppModule()
    };
  }

  private Modules() {
    // No instances.
  }
}

Inside the release sourceset, I define the same thing minus the DebugAppModule:

public final class Modules {
  public static Object[] list(MyApplication app) {
    return new Object[] {
            new AppModule(app)
    };
  }

  private Modules() {
    // No instances.
  }
}

Deeper down my dependency graph, I create a MockRestAdapter that I can use when running the debug version:

@Module(
    complete = false,
    library = true,
    overrides = true
)
public final class DebugApiModule {

  @Provides @Singleton Endpoint provideEndpoint(@ApiEndpoint StringPreference apiEndpoint) {
    return Endpoints.newFixedEndpoint(apiEndpoint.get());
  }

  @Provides @Singleton MockRestAdapter provideMockRestAdapter(RestAdapter restAdapter, SharedPreferences preferences) {
    MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
    AndroidMockValuePersistence.install(mockRestAdapter, preferences);
    return mockRestAdapter;
  }

  @Provides @Singleton MyApi provideMyApi(RestAdapter restAdapter, MockRestAdapter mockRestAdapter,
                                          @IsMockMode boolean isMockMode, MockMyApi mockService) {
    if (isMockMode) {
      return mockRestAdapter.create(MyApi.class, mockService);
    }
    return restAdapter.create(MyApi.class);
  }

}

But while I'm running tests, I would like to override the DebugApiModule with a TestApiModule that looks like this:

@Module(
    complete = false,
    library = true,
    overrides = true
)
public final class TestApiModule {

  @Provides @Singleton Endpoint provideEndpoint(@ApiEndpoint StringPreference apiEndpoint) {
    return Endpoints.newFixedEndpoint(apiEndpoint.get());
  }

  @Provides @Singleton MockRestAdapter provideMockRestAdapter(RestAdapter restAdapter, SharedPreferences preferences) {
    MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
    mockRestAdapter.setDelay(0);
    mockRestAdapter.setErrorPercentage(0);
    mockRestAdapter.setVariancePercentage(0);
    return mockRestAdapter;
  }

  @Provides @Singleton MyApi provideMyApi(MockRestAdapter mockRestAdapter, MockHnApi mockService) {
      return mockRestAdapter.create(MyApi.class, mockService);
  }

}

What's the best way to accomplish this? Do I need to create a TestAppModule like this:

public final class Modules {
  public static Object[] list(MyApplication app) {
    return new Object[] {
        new AppModule(app),
        new TestAppModule()
    };
  }

  private Modules() {
    // No instances.
  }
}

And replace all of the DebugFooModule with TestFooModules? If so, how do I get around the fact that Modules.java is duplicated? Or am I way off base?

EDIT: SOLUTION

What I ended up doing is replacing the Application-level graph (where the MockRestAdapter gets created) during my test setUp

protected void setUp() throws Exception {
  super.setUp();
  HnApp app = HnApp.getInstance();
  app.buildObjectGraphAndInject(TestModules.list(app));
  getActivity();
}

回答1:


What I've done this far (and I'm not entirely sure this will scale yet) is create a static test module class in my tests.

   @Module(
            injects = {
                    SharedPreferences.class
    })

      static  class TestModule {
            @Provides @Singleton SharedPreferences provideSharedPreferences(){
                return Mockito.mock(SharedPreferences.class);
            }
    }

And in the setup() method I inject the test module

  @Before
    public  void setUp(){
        ObjectGraph.create(new TestModule()).inject(this);
}

I then @Inject the mocked class:

@Inject SharedPreferences sharedPreferences;


来源:https://stackoverflow.com/questions/25271721/overriding-debug-module-in-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!