Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE when spying on android.os.Handler class on Android API 28+ #1775

Open
naquden opened this issue Sep 6, 2019 · 0 comments
Open

NPE when spying on android.os.Handler class on Android API 28+ #1775

naquden opened this issue Sep 6, 2019 · 0 comments

Comments

@naquden
Copy link

naquden commented Sep 6, 2019

When using Mockito.spy(handler) the mQueue member variable is not copied over the to spy object.

Mockito :org.mockito:mockito-android
Mockito version used: 3.0.0

Sample:
Handler preHandler = new Handler(testApplication.getMainLooper());
spyHandler = spy(preHandler);

^ The preHandler has a mQueue variable set but the spyHandler does not.
When going through source code I noticed that the fields returned from Handler.class.getDeclaredFields() differs from API 28+ and API < 28.
This causes the org.mockito.internal.util.reflection.LenientCopyTool#copyValues to fail to copy the mQueue variable from the spiedInstance on API 28+

It's important for the the mQueue to be copied as well since it's not null checked in the Handler class final methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant