Reputation: 39
I am trying to mock an NFC Tag with an in-app developer mocking tool. The current implementation uses reflection and is targeting API 27. Migrating to API 28 with AndroidX, the method createMockTag
is not found. I notice that it's public static
in the Android code, but is annotated with @Hide
, meaning that I can't access it. I have found a way, however, to create a Tag from a Parcel, but I have not found anywhere a simple way to do it. I can create my own Parcelable class, but when calling in.readInArray(...)
in the constructor, it asks for a parameter that I don't have. I will attach what it looks like know and what I'd like it to look like.
This is a big issue because we need to mock NFC Tags, but there doesn't seem to be a way to access the method. And I've also tried to copy the Tag.java
class into my project, but it can't access certain seemingly-internal classes such as INfcTag
and the enums in TagTechnology
. Has anyone else come across this and can please help me? Thank you.
// Tag mockTag = Tag.CREATOR.createFromParcel(); // I'd like to create one here
Method createMockTag = Tag.class.getMethod("createMockTag", byte[].class, int[].class, Bundle[].class);
scanIntent.putExtra(NfcAdapter.EXTRA_TAG, (Tag) createMockTag.invoke(Tag.class, tagId, new int[]{}, new Bundle[]{}));
Here are links I've looked at:
Upvotes: 2
Views: 763
Reputation: 1706
You don't want to copy the code from the original, that's not how mocking works. If this specific method has been marked with @Hide it likely means that its being deprecated. That likely means there is an alternative to this method in API 28+.
The @Hide is likely a result of them not being able to fully deprecate it just yet, but will likely in the future. Here is the current reference - https://developer.android.com/reference/android/nfc/package-summary.
In general though, say your using Mockito - https://static.javadoc.io/org.mockito/mockito-core/2.25.1/org/mockito/Mockito.html#2 - just stub the mock.
For stubbbing static methods, looks like there are a couple of threads out there - for the actual feature request: https://github.com/mockito/mockito/issues/1013
And workaround: Mocking static methods with Mockito not sure how relevant the workaround is anymore (its somewhat old).
Upvotes: 1