Android Unit Tests with Dagger 2

前端 未结 5 1720
死守一世寂寞
死守一世寂寞 2020-12-13 06:08

I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for inst

5条回答
  •  离开以前
    2020-12-13 06:24

    If you are using dagger2 with Android, you can use app flavours for providing mocking resources.

    See here for a demo of flavours in mock testing(without dagger): https://www.youtube.com/watch?v=vdasFFfXKOY

    This codebase has an example: https://github.com/googlecodelabs/android-testing

    In your /src/prod/com/yourcompany/Component.java you provide your production components.

    In your /src/mock/com/yourcompany/Component.java you provide your mocking components.

    This allows you create builds of your app with or without mocking. It also allows parallel development (backend by one team, frontend app by another team), you can mock until api methods are avilable.

    How my gradle commands look (its a Makefile):

    install_mock:
        ./gradlew installMockDebug
    
    install:
        ./gradlew installProdDebug
    
    test_unit:
        ./gradlew testMockDebugUnitTest
    
    test_integration_mock:
        ./gradlew connectedMockDebugAndroidTest
    
    test_integration_prod:
        ./gradlew connectedProdDebugAndroidTest
    

提交回复
热议问题