【问题标题】:Testing with Dagger 2 using separate component configurations in Android在 Android 中使用单独的组件配置使用 Dagger 2 进行测试
【发布时间】:2016-07-09 03:54:07
【问题描述】:

Dagger 2 documentation suggests providing different configurations 用于测试和生产,使用interface 用于ProductionComponentTestComponent,如下所示:

@Component(modules = {
  OAuthModule.class, // real auth
  FooServiceModule.class, // real backend
  OtherApplicationModule.class,
  /* … */ })
interface ProductionComponent {
  Server server();
}

@Component(modules = {
  FakeAuthModule.class, // fake auth
  FakeFooServiceModule.class, // fake backend
  OtherApplicationModule.class,
  /* … */})
interface TestComponent extends ProductionComponent {
  FakeAuthManager fakeAuthManager();
  FakeFooService fakeFooService();
}

假设我们有一个使用 ProductionComponent 的 Android 活动 (MyApp):

public class MyApp extends Application {
    private ProductionComponent component;

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

        component = ProductionComponent.builder()
                .serverModule(new ServerModule())
                .build();
    }
}

一般来说,在 Android 集成测试中使用 DaggerTestComponent.builder() 而不是 ProductionComponent.builder() 的最佳方式是什么?

我不确定如何使用假货;我应该在/androidTest 哪个extends MyApp 中进行新活动吗?或者我应该在设置测试时使用 getter/setter 将新的 DaggerTestComponent 传递给 MyApp

【问题讨论】:

标签: java android integration-testing dagger-2


【解决方案1】:

robolectricMockito 一起使用怎么样?

您可以使用 @Test 而不使用 AndroidTest 来制作 JUnit 测试代码。 我认为您使用 Mockito 制作了测试匕首应用程序。

参考文献

http://alexzh.com/tutorials/android-testing-mockito-robolectric/ http://sdudzin.blogspot.kr/2011/01/easy-unit-testing-for-android.html

【讨论】:

    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2020-09-02
    • 1970-01-01
    • 2019-01-22
    • 2020-01-17
    • 1970-01-01
    相关资源
    最近更新 更多