简体   繁体   中英

mock final classes for both unit and UI tests

I am trying to write both unit and espresso UI test for my android app, build with kotlin. Since in kotlin every class is final by default so to mock a final class I have used mockInline library which supports mocking final classes. Since I have to write both unit and UI tests. I have added the library in the following way

testImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'

But on running UI tests It is throwing

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

Is there a way to use the mockInline library to mock final classes for both unit and UI tests

We can not mock final classes for UI Tests(Android Test) using mockito inline plugin as mentioned on official issue tracker here

The reason behind that is

"There is no real possibility to make this work in Android at the moment as it lacks the instrumentation API on top of which we are operating. The Android VM is not a standard VM and only implements a subset of the Java specification. As long as Google does not choose to extend its JVM, I am afraid that this feature will not work."

as mentioned here

To mock final classes for android Tests

  • We can use DexOpener library with the ability to mock final classes in Android.

  • We can also use Kotlin Open Plugin which makes all final classes open without explicit open keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM