Source:
public void testGCMJourneyMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationDetails.NotificationDetailsBuilder notificationDetailsBuilder = NotificationDetails.builder() .from("12345") .bundle(buildJourneyPushBundle()) .serviceClass(Service.class) .intentAction(GCM_INTENT_ACTION); NotificationClient.PinpointPushResult pushResult = target.handlePushNotification(notificationDetailsBuilder.build()); assertEquals(NotificationClient.PinpointPushResult.OPTED_OUT, pushResult); ArgumentCaptor<AnalyticsEvent> eventCaptor = ArgumentCaptor.forClass(AnalyticsEvent.class); verify(mockEventRecorder, times(1)).recordEvent(eventCaptor.capture()); final AnalyticsEvent receivedEvent = eventCaptor.getAllValues().get(0); assertThat(receivedEvent.getEventType(), is("_journey.received_background")); assertTrue(receivedEvent.getEventTimestamp() > 0); for (Map.Entry<String, String> entry : receivedEvent.getAllAttributes().entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } assertThat(receivedEvent.getAllAttributes().size(), is(8)); assertThat(receivedEvent.getAttribute("isOptedOut"), is("true")); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false")); assertThat(receivedEvent.getAttribute("journey_id"), is("fake_journey_id")); assertThat(receivedEvent.getAttribute("journey_activity_id"), is("fake_journey_activity_id")); assertThat(receivedEvent.getAttribute("journey_run_id"), is("fake_journey_run_id")); assertThat(receivedEvent.getAttribute("feedback"), is("random_feedback")); assertThat(receivedEvent.getAttribute("endpoint_id"), is("fake_endpoint_id")); assertThat(receivedEvent.getAttribute("user_id"), is("fake_user_id")); assertThat(receivedEvent.getAllMetrics().size(), is(0)); }

Target:
public void testGCMJourneyMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { final AnalyticsEvent expectedEvent = new AnalyticsClient(mockPinpointContext).createEvent("_journey.received_background") .withAttribute("journey_id", "fake_journey_id") .withAttribute("journey_activity_id", "fake_journey_activity_id") .withAttribute("journey_run_id", "fake_journey_run_id") .withAttribute("feedback", "random_feedback") .withAttribute("endpoint_id", "fake_endpoint_id") .withAttribute("user_id", "fake_user_id") .withAttribute("isOptedOut", "true") .withAttribute("isAppInForeground", "false"); final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationDetails.NotificationDetailsBuilder notificationDetailsBuilder = NotificationDetails.builder() .from("12345") .bundle(buildJourneyPushBundle()) .serviceClass(Service.class) .intentAction(GCM_INTENT_ACTION); NotificationClient.PushResult pushResult = target.handleNotificationReceived(notificationDetailsBuilder.build()); assertEquals(NotificationClient.PushResult.OPTED_OUT, pushResult); ArgumentCaptor<AnalyticsEvent> eventCaptor = ArgumentCaptor.forClass(AnalyticsEvent.class); verify(mockEventRecorder, times(1)).recordEvent(eventCaptor.capture()); final AnalyticsEvent receivedEvent = eventCaptor.getAllValues().get(0); assertTrue(receivedEvent.getEventTimestamp() > 0); assertEquals("_journey.received_background", receivedEvent.getEventType()); assertEquals(expectedEvent.getAllAttributes(), receivedEvent.getAllAttributes()); }