tdrhq / eyepatch

Mock static methods in Android (Dalvik/ART) tests
MIT License
1 stars 0 forks source link

Other static mocking efforts for Android #2

Open moltmann opened 6 years ago

moltmann commented 6 years ago

Please take a look at:

Maybe we can do some integration

tdrhq commented 6 years ago

@moltmann Thanks for the links! (And thanks very much for maintaining dexmaker which was crucial for this project!)

I suspect EyePatch would never be able to do something like MockitoSession session = mockitoSession().staticSpy(C.class).startMocking();, since it uses a custom class loader instead of JVMTI (Unless I add the EyePatch overhead on every class in the system). Of course, there's the advantage that EyePatch works for older devices (I think I can make it work upto ICS, perhaps even GB), but eventually people are going to favor the ExtendedMockito API. I wonder if there's a happy middle ground where we can make both work, but it isn't immediately obvious to me how that would look like.

moltmann commented 6 years ago

I think adding the method entry hooks during load time is a viable solution. Once we have the entry hooks they stay dormant until triggered. Of course we only want to enable this for tests and maybe restrict it to certain classes, but I think this would then just become a natural backwards compat story.

tdrhq commented 6 years ago

@moltmann speaking of, I could use your help with something :) (and it will be required if I need to add hooks to every class):

I still haven't figured out a reasonble way to copy bytecode from the original class to the patched class. Right now I have mechanisms to shadow entire classes, but not partial shadows. Does dexmaker provide something for this? Do you have a suggestion as to how I can do this without having to parse out the .dex files manually?

tdrhq commented 6 years ago

@moltmann update: I spent the last few weeks building out support for spying/partial mocking, and it now appears to work. https://github.com/tdrhq/eyepatch/blob/master/eyepatch/src/androidTest/java/com/tdrhq/eyepatch/dexmagic/ShadowClassHandlerTest.java#L29

Given that I can do this now, I might be able to build the API you were pointing at, there might be some extra overhead to add the "entry hooks" to every class, but I think it should be doable.

tdrhq commented 6 years ago

(I had to go through a mind boggling number of hacks to get here)