Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android kotlin mockMaker issues

Hi i am trying to mock a final class(as all classes in kotlin are final by default) and added the following dependencies in my gradle:

testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation "org.mockito:mockito-android:2.13.0"
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation "org.mockito:mockito-core:2.13.0"
//testImplementation 'io.mockk:mockk:1.8'
testImplementation 'org.assertj:assertj-core:3.8.0'

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "org.mockito:mockito-core:2.13.0"
androidTestImplementation "org.mockito:mockito-android:2.13.0"
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"

the mockito-inline is supposed to enable you to mock a final kotlin class and so i added to both my java unit test and my instrumental test using testImplementation and androidTestImplementation

On building the project i get the following error:

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'

Any ideas whats going on? if i remove the androidTestImplementation of the mockitio inline, it compiles fine but when running intrumental test i get the mockito error saying it cannot mock a final class.

like image 272
Jonathan Avatar asked May 21 '18 10:05

Jonathan


2 Answers

In order to be able to mock final classes in Kotlin, you’ll need to create a file org.mockito.plugins.MockMaker (literally) that contains only this line

mock-maker-inline

and place it into test/resources/mockito-extensions.

For more info please read https://antonioleiva.com/mockito-2-kotlin/.

like image 76
Demigod Avatar answered Sep 23 '22 10:09

Demigod


mockito-inline won't work for instrumentation tests. In order to be able to mock final classes in instrumentation tests you just need to include the following line only:

androidTestImplementation com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.0

Refer to its Github page for more details

This thread might also be useful.

like image 25
Nijat Ahmadli Avatar answered Sep 20 '22 10:09

Nijat Ahmadli