You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
06-10 16:00:53.355 27681 27703 E TestRunner: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder android.content.IIntentSender.asBinder()' on a null object reference
06-10 16:00:53.355 27681 27703 E TestRunner: at android.app.PendingIntent.hashCode(PendingIntent.java:1192)
06-10 16:00:53.355 27681 27703 E TestRunner: at java.util.HashMap.hash(HashMap.java:338)
06-10 16:00:53.355 27681 27703 E TestRunner: at java.util.HashMap.put(HashMap.java:611)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.InlineStaticMockMaker.createMock(InlineStaticMockMaker.java:167)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.MockMakerMultiplexer.createMock(MockMakerMultiplexer.java:65)
06-10 16:00:53.355 27681 27703 E TestRunner: at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
06-10 16:00:53.355 27681 27703 E TestRunner: at org.mockito.internal.MockitoCore.mock(MockitoCore.java:69)
06-10 16:00:53.355 27681 27703 E TestRunner: at org.mockito.Mockito.mock(Mockito.java:1905)
06-10 16:00:53.355 27681 27703 E TestRunner: at org.mockito.Mockito.mock(Mockito.java:1814)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder.lambda$mockStatic$0(StaticMockitoSessionBuilder.java:58)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.extended.-$$Lambda$StaticMockitoSessionBuilder$AoyQNqvLdWW5bc2GJ4H8RWXvBuo.get(Unknown Source:2)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.extended.StaticMockitoSession.mockStatic(StaticMockitoSession.java:98)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.dx.mockito.inline.extended.StaticMockitoSessionBuilder.startMocking(StaticMockitoSessionBuilder.java:144)
06-10 16:00:53.355 27681 27703 E TestRunner: at com.android.networkstack.tethering.TetheringNotificationUpdaterTest.setUp(TetheringNotificationUpdaterTest.kt:148)
I have looked into the NPE, the problem is that InlineStaticMockMaker uses HashMap to store mock object data that will call to real PendingIntent.hascode() when trying to put mock PendingIntent object as a HashMap key.
To solve the NPE, I suggest InlineStaticMockMaker may use MockMap like InlineDexmakerMockMaker to store mock object data safely.
/** * All currently active mock markers. We modify the class's byte code. Some objects of the class * are modified, some are not. This list helps the {@link MockMethodAdvice} help figure out if a * object's method calls should be intercepted. */privatefinalHashMap<Object,InvocationHandlerAdapter>markerToHandler=newHashMap<>();privatefinalMap<Class,Object>classToMarker=newHashMap<>();/***** Should use MockMap instead. *****/
....
@Overridepublic<T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {Class<T>typeToMock=settings.getTypeToMock();if(!typeToMock.equals(mockingInProgressClass.get())||Modifier.isAbstract(typeToMock.getModifiers())){returnnull;}
Set<Class<?>>interfacesSet=settings.getExtraInterfaces();InvocationHandlerAdapterhandlerAdapter=newInvocationHandlerAdapter(handler);classTransformer.mockClass(MockFeatures.withMockFeatures(typeToMock,interfacesSet));Instantiatorinstantiator=Mockito.framework().getPlugins().getDefaultPlugin(InstantiatorProvider2.class).getInstantiator(settings);Tmock;try{mock=instantiator.newInstance(typeToMock);}catch(org.mockito.creation.instance.InstantiationExceptione){thrownewMockitoException("Unable to create mock instance of type '"+typeToMock.getSimpleName()+"'",e);}if(classToMarker.containsKey(typeToMock)){thrownewMockitoException(typeToMock+" is already mocked");}classToMarker.put(typeToMock,mock);/***** Got NPE here *****/markerToHandler.put(mock,handlerAdapter);returnmock;}
The text was updated successfully, but these errors were encountered:
I tried to use
mockStatic
to mock PendingIntent class in my test but it got NPE immediately after calledstartMonitoring()
.Code:
Logs:
I have looked into the NPE, the problem is that
InlineStaticMockMaker
usesHashMap
to store mock object data that will call to realPendingIntent.hascode()
when trying to put mockPendingIntent
object as aHashMap
key.To solve the NPE, I suggest
InlineStaticMockMaker
may use MockMap likeInlineDexmakerMockMaker
to store mock object data safely.dexmaker-mockito-inline-extended/src/main/java/com/android/dx/mockito/inline/InlineStaticMockMaker.java
The text was updated successfully, but these errors were encountered: