[英]Injecting test module with dagger2
I use Dagger2 in my android app. 我在我的Android应用程序中使用Dagger2。 Basically I inject a HttpClient
(interface) in MainActivity
. 基本上我在MainActivity
注入一个HttpClient
(接口)。
@Module
public class MainActivityModule{
@Provides public HttpClient providesHttpComponent(){
return new RealHttpClient();
}
}
@Component( modules = MainActivityModule.class )
public interface MainActivityComponent {
public MainActivity injectActivity(MainActivity);
}
public class MainActivity extends Activity {
public void onCreate(Bundle saved){
super.onCreate();
injectDependencies();
}
protected void injectDependencies(){
Dagger_MainActivityComponent
.builder()
.mainActivityComponent( new MainActivityModule())
.build()
.injectActivity(this);
}
}
So far so good, that works like expected. 到目前为止一切顺利,效果如预期。 Now I want to write some unit tests (not android instrumentation tests) for MainActivity
where I want to use TestMainActivityModule
instead of MainActivityModule
. 现在我想为MainActivity
编写一些单元测试(不是android工具测试),我想使用TestMainActivityModule
而不是MainActivityModule
。
@Module (overrides = true )
public class TestMainActivtiyModule extends MainActivityModule {
@Provides public HttpClient(){
return new MockHttpClient();
}
}
My question is: How do I force MainActivity
to use TestMainActivitiyModule
instead of MainActivityModule
? 我的问题是:如何强制MainActivity
使用TestMainActivitiyModule
而不是MainActivityModule
? Is there a good solution for that? 那有一个很好的解决方案吗?
My current approach is to use inheritance and to override getModule()
, something like this 我目前的方法是使用继承并覆盖getModule()
,就像这样
public class TestMainActivity extend MainActivity {
@Override
protected void injectDependencies(){
Dagger_MainActivityComponent
.builder()
.mainActivityComponent( new TestMainActivtiyModule())
.build()
.injectActivity(this);
}
}
and to run unit test against TestMainActivity
instead of MainActivity
. 并针对TestMainActivity
而不是MainActivity
运行单元测试。
I guess it works, but one of the problems I'm facing with this approach is that I can't start TestMainActivity
with an Intent
because I can't specify it in AndroidManifest.xml
我猜它有效,但我遇到的问题之一就是我无法用Intent
启动TestMainActivity
,因为我无法在AndroidManifest.xml
指定它
Does anyone know a better approach for unit testing with dagger2 on android? 有没有人知道在Android上使用dagger2进行单元测试的更好方法?
The approach I've started using has involved maintaining two modules (one for the app, one for testing) in parallel build variants (ex: app
and integration
). 我开始使用的方法涉及在并行构建变体(例如: app
和integration
)中维护两个模块(一个用于app,一个用于测试)。 Still not sure how well that solution scales so YMMV. 仍然不确定该解决方案如何很好地扩展YMMV。 I'd be very happy to see a better solution! 我很高兴看到更好的解决方案!
This is also a great read: http://engineering.circle.com/instrumentation-testing-with-dagger-mockito-and-espresso/ 这也很棒: http : //engineering.circle.com/instrumentation-testing-with-dagger-mockito-and-espresso/
I would really suggest you to check this boilerplate since it is fully based on DI using Dagger2. 我真的建议你检查这个样板,因为它完全基于使用Dagger2的DI。 It also shows how you can replace your dependencies in the test environment in a very neat way. 它还展示了如何以非常简洁的方式替换测试环境中的依赖项。
The dependencies currently handled by the boiler plate are the following: 锅炉板当前处理的依赖关系如下:
The power of dependency injection comes really handy especially for testing since you can easily switch your dependencies in the test environment to dummy dependencies. 依赖注入的强大功能非常方便,特别是对于测试,因为您可以轻松地将测试环境中的依赖项切换为虚拟依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.