tmurakami / dexopener

An Android library that provides the ability to mock your final classes on Android devices.
Apache License 2.0
113 stars 4 forks source link

Cannot mock final classes declared on on pure Java / Kotlin modules #20

Closed JorgeCastilloPrz closed 6 years ago

JorgeCastilloPrz commented 6 years ago

If you have a class defined on a pure Java or Kotlin library module (not Android) , let's say:core, then you write a UI test on your app module (the android application module) which uses that dependency, dexopener fails.

Cannot mock/spy class com.conversations.usecase.SendSyncServiceCommand
Mockito cannot mock/spy because :
- final class
tmurakami commented 6 years ago

The Limitations of the README says the following:

The final classes you can mock are only those under the package of your app's BuildConfig. For example, if the FQCN of your BuildConfig is foo.bar.BuildConfig, you can mock only the final classes belonging to foo.bar.**. Therefore, you cannot mock final classes of both Android system classes and third-party libraries.

For example, if you have two modules like:

the classes you want to mock must be under the package indicated by the applicationId of app even though they belong to lib.

If the applicationId of app isfoo.bar, you will need to put the classes belonging to lib into the package under foo.bar (e.g. foo.bar.lib).

tmurakami commented 6 years ago

I have just published a multiproject example. https://github.com/tmurakami/dexopener/tree/master/dexopener-example/multiproject

JorgeCastilloPrz commented 6 years ago

Alright, that makes a lot of sense now actually. My lib has a different package name. I'll try what you say and report back. Thanks for uploading that sample, will be very handy.

JorgeCastilloPrz commented 6 years ago

Your suggestion worked perfectly. I renamed application Id and package names for all the modules to be the same and then everything worked smoothly. Closing this issue now. Thanks for your help!