How to mock final class with Mockito 2 on Java Module in Android project?

前端 未结 2 1583
生来不讨喜
生来不讨喜 2020-12-10 20:30

I have a Android Clean Architecture project write in Kotlin with 3 modules:

  • data (Android Library)
  • domaine (Java Library)
  • presentation (Andro
相关标签:
2条回答
  • 2020-12-10 21:21

    Mockito's MockMaker can only be used for unit tests (run on JVM). For those who are having problems with mocking Kotlin classes in instrumentation tests (androidTest), try using the DexOpener library. It makes Kotlin classes, properties and methods open, which allows their mocking.

    0 讨论(0)
  • 2020-12-10 21:32

    You can use the inline mocking method by default, by changing your Gradle dependency from the normal Mockito dependency:

    compile "org.mockito:mockito-core:$mockito_version"
    

    ... to the following:

    compile "org.mockito:mockito-inline:$mockito_version"
    

    This way you won't have to rely on activating inline mocking with the "file in the resources folder" method, which I have found to be flaky sometimes.

    0 讨论(0)
提交回复
热议问题