PushReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (INTENT_ACTION_REGISTRATION.equals(action)) { String registrationId = intent.getStringExtra(INTENT_EXTRA_REGISTRATION); Push.getInstance().onTokenRefresh(registrationId); } else if (INTENT_ACTION_RECEIVE.equals(action)) { Push.getInstance().onMessageReceived(context, intent); } }  @Override void onReceive(Context context, Intent intent);  }
@Test public void onTokenRefresh() { Intent intent = mock(Intent.class); when(intent.getAction()).thenReturn(PushReceiver.INTENT_ACTION_REGISTRATION); when(intent.getStringExtra(PushReceiver.INTENT_EXTRA_REGISTRATION)).thenReturn("mockToken"); new PushReceiver().onReceive(mock(Context.class), intent); verify(mPush).onTokenRefresh("mockToken"); verifyNoMoreInteractions(mPush); }
@Test public void onMessageReceived() { Context context = mock(Context.class); Intent intent = mock(Intent.class); when(intent.getAction()).thenReturn(PushReceiver.INTENT_ACTION_RECEIVE); new PushReceiver().onReceive(context, intent); verify(mPush).onMessageReceived(context, intent); verifyNoMoreInteractions(mPush); }
@Test public void onInvalidIntent() { new PushReceiver().onReceive(mock(Context.class), mock(Intent.class)); verifyZeroInteractions(mPush); }