【发布时间】:2016-07-09 03:54:07
【问题描述】:
Dagger 2 documentation suggests providing different configurations 用于测试和生产,使用interface 用于ProductionComponent 和TestComponent,如下所示:
@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?
【问题讨论】:
-
在blog.egorand.me/…找到了一个非常有用的解决方案
标签: java android integration-testing dagger-2