Open krazykira opened 6 years ago
Hi, right now I think that the only solution available is to declare all three fields in the test, obviously it's a decent workaround only if there aren't other dependencies. Something like this:
val networkSource = NetworkSource()
val diskSource = DiskSource()
val productRepository: ProductRepository = spy(ProductRepositoryImpl(networkSource, diskSource))
DaggerMock will replace all the three objects in the module and it should work. However this is an use case that can happen and something in the rule that allows to decorate an existing object can be useful. I'll try to add it in a future release, thanks for the suggestion!
About the final class issue, this is a problem related to the mockito usage on Kotlin, my suggestion to solve it is to use mockito inline dependency in JVM tests or Kotlin all open compiler plugin in Espresso tests.
Thanks for the response, maybe i can help with a PR once i have some time.
About the final class issue, this is a problem related to the mockito usage on Kotlin, my suggestion to solve it is to use mockito inline dependency in JVM tests or Kotlin all open compiler plugin in Espresso tests.
I don't understand whats the difference and why it works in main
package but doesn't work on androidTest
package. I am already using dexOpener and i don't think it is an issue caused by it. Maybe shed some light on whats difference it makes when the file is in main
package and when it is in androidTest
package
The instrumentations test are not executed on the jvm so mockito inline is not enough. You need to use kotlin all open or something similar. I think that the reason is something related to the mockito implementation and the differences between jvm and dalvik/art.
The instrumentations test are not executed on the jvm so mockito inline is not enough
i am not using mockito inline, rather using Dexopener. DaggerMockRule is what throws this error.
@fabioCollini i added a PR which adds the spy feature to DaggerMock
The error is thrown by DaggerMock because internally it uses Mockito. DexOpener should work, maybe you are defining your module in a package that it's not managed by DexOpener. You can find more details here: https://github.com/tmurakami/dexopener#limitations
The package name of both the main
and androidTest
package are the same and the path for DexOpener
is also correct. Maybe i should share the code with you to give you a better overview
Thank you very much for the wonderful library. I have been playing around with it and i came across some limitations recently when i was trying to implement a UI test in
androidTest
package. i am usingdagger.android
and i want to inject some classes asspy
without specifically creating the test modules manuallyExample What i want to do is that i have a
ProductRepository
and want to stub a few methods instead of mocking the wholeProductRepository
. I know something like this (Partial mocking) is possible by usingspy
instead ofmock
. My product repository has 2 dependencies and i want dagger to provide them instead of providing them manually for my spy object. Below is the code for my test project.CustomTestRunner
TestApplication
TestAppComponent
TestDaggerMockRule
I am unable to find a way to do this easily using
DaggerMock
. The only option i see is manually overriding theSpyRepositoryModule
which providesSpyProductRepository
. Can you tell me if there is any other simple way to do this without rewriting the modules ? (Which will provide me spies instead of real objects)SpyProductRepository
I would really like to not do this
Also another thing, If i remove
open
fromSpyProductRepository
which is located inside theandroidTest
package then i am getting the following error. It works fine if i move this class tomain
package where the app code resides then the error goes away. Super confusing :(Suggestion Maybe the
DaggerMockRule
can take an boolean as constructor to provide spies for all dependencies or provide a method where we couldoverride
to provide all the dependencies that we couldspy