6

I have a android module that uses Kotlin and by using kotlin-all open class I am making sure that I still can mock my Kotlin classes with Mockito. The problem is with this approach it makes my Kotlin classes open outside testing as well and now someone can extend this class witch I don't want to allow.

This is my configuration

apply plugin: "kotlin-allopen"

allOpen {
    annotation("com.me.Open")
}

I am looking for a way to enable this plugin only for testing, allowing mock Kotlin classes while stoping extension from Kotlin non-open class. Any Idea?

BTW, I don't want to use mockito-inline plugin since it slows down our tests by noticeable measure.

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • Have you considered redesigning your dependencies to depend only on interfaces? This is usually a best practise precisely for what your facing - being able to replace the dependency with another implementation (mocks in this case) without changing the code. You can keep your classes closed then. – Fred Dec 26 '19 at 16:18
  • 2
    Try adding debug/release annotation like described here https://github.com/mockito/mockito/issues/1082#issuecomment-301646307 – Lena Bru Feb 02 '20 at 10:41

0 Answers0