How to mock final class with Mockito 2 on Java module in Android project?
I have Android Pure Architecture Project write in Kotlin 3 modules:
- data (android library)
- domaine (Java library)
- presentation (Android application)
Each of the 3 modules has unit tests written with junit . But with Kotlin, every class is final by default. I quickly had a problem: How to mock the final class with mockito
You can now use Mockito 2
This can be done using the mockito extension engine by creating a file /mockito-extensions/org.mockito.plugins.MockMaker
containing one line:
mock-maker-inline
This solution works very well on data module (Android Library) and presentation module (Android app) , but does not work on my domaine module (Java library) .
I know this question has already been asked ( How to mock the latest class with mockito , Mock objects calling final classes static methods with Mockito ), but I didn't find the answer I'm looking for.
source to share
By default, you can use the built-in mock method by changing the Gradle dependency from the regular Mockito dependency:
compile "org.mockito:mockito-core:$mockito_version"
... to the next:
compile "org.mockito:mockito-inline:$mockito_version"
That way, you don't have to rely on the inline mocking activation of the file-in-assets-folder method, which I sometimes found to be broken sometimes.
source to share