Android Mocking a Dagger2 injected dependency for a Espresso test

后端 未结 3 1968
太阳男子
太阳男子 2021-02-15 11:40

I have a heavily dependency injected (dagger2) application. I would like to run an espresso test without having the test navigate through the whole application

3条回答
  •  鱼传尺愫
    2021-02-15 12:43

    Seems like architecture problem rather than a minor issue.

    First off I wouldn't create a static class for calling dagger2 component my approach would be more android centric, I mean using singleton application with all of its bells and whistles.

    anyway... Best way to run testing without running whole work-flow is to separate your project into two different projects:

    1- UI app, your android activities and fragments, etc...

    2-Logic Module using an enterprise architecture say MVP/MVC/MVVM (it should be a different project inside your android studio)

    Where should you use dagger? inside your UI app for sticking the logic module into your UI.

    How can you test different part of app (Logic Module)? since you separated your logic into different part writing tests for them would be much easier even though you are not gonna need Esperesso anymore. simple unit testing wiht JUnit and Mockito can help you without running the whole work-flow.

    note that you shouldn't have any kind of logic inside your UI app.

    My opinion is Clean Architecture : https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

    I have a simple scaffold for above approach in my github you can read that too, well if you fancy : https://github.com/vahidhashemi/android_clean_architecture

提交回复
热议问题