简体   繁体   English

用dagger2注入测试模块

[英]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 ). 我开始使用的方法涉及在并行构建变体(例如: appintegration )中维护两个模块(一个用于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: 锅炉板当前处理的依赖关系如下:

  • Database dependency: encapsulates all the database operations. 数据库依赖性:封装所有数据库操作。
  • Shared preferences dependency: deals with shared preferences. 共享首选项依赖项:处理共享首选项。
  • Local files dependency: which deals with saving on files. 本地文件依赖:处理文件保存。
  • Analytics dependency: covers all the operation of reporting events to your analytics backend (GA, Segment, FB, Flurry ..) Analytics依赖:涵盖向分析后端报告事件的所有操作(GA,Segment,FB,Flurry ..)
  • Logging dependency: encapsulates all the operations related to logging to your console 记录依赖性:封装与记录到控制台相关的所有操作
  • Api dependency: encapsulates all the API related operations Api依赖:封装所有与API相关的操作

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM