public void appendWarnings(Failure failure, String warnings) { if (isEmpty(warnings)) { return; } //TODO: this has to protect the use in case jUnit changes and this internal state logic fails Throwable throwable = (Throwable) Whitebox.getInternalState(failure, "fThrownException"); String newMessage = "contains both: actual test failure *and* Mockito warnings.\n" + warnings + "\n *** The actual failure is because of: ***\n"; ExceptionIncludingMockitoWarnings e = new ExceptionIncludingMockitoWarnings(newMessage, throwable); e.setStackTrace(throwable.getStackTrace()); Whitebox.setInternalState(failure, "fThrownException", e); }
@Test public void cancel() throws Exception { JobCoordinator coordinator = Mockito.mock(JobCoordinator.class); Map<Long, JobCoordinator> map = Maps.newHashMap(); long jobId = 1L; map.put(jobId, coordinator); Whitebox.setInternalState(mJobMaster, "mIdToJobCoordinator", map); mJobMaster.cancel(jobId); Mockito.verify(coordinator).cancel(); }
public void testFailedPutUser() throws Exception { final ContentProviderStorage storage = new ContentProviderStorage(getProviderMockContext(), "testPut_Device", TrayStorage.Type.USER); TrayProviderHelper mockHelper = mock(TrayProviderHelper.class); when(mockHelper.persist(any(Uri.class), anyString(), anyString())).thenReturn(false); Whitebox.setInternalState(storage, "mProviderHelper", mockHelper); assertFalse(storage.put(TEST_KEY, TEST_STRING)); assertDeviceDatabaseSize(0); assertUserDatabaseSize(0); }
public void testFailedPutDevice() throws Exception { final ContentProviderStorage storage = new ContentProviderStorage(getProviderMockContext(), "testPut_Device", TrayStorage.Type.DEVICE); TrayProviderHelper mockHelper = mock(TrayProviderHelper.class); when(mockHelper.persist(any(Uri.class), anyString(), anyString())).thenReturn(false); Whitebox.setInternalState(storage, "mProviderHelper", mockHelper); assertFalse(storage.put(TEST_KEY, TEST_STRING)); assertDeviceDatabaseSize(0); assertUserDatabaseSize(0); }
doCallRealMethod().when( rest ).searchForHeaders( any() ); setInternalState( rest, "meta", meta ); setInternalState( rest, "data", data );
private void changeInternalStateForTesting(PhoenixResultSet rs) { // get and set the internal state for testing purposes. ReadMetricQueue testMetricsQueue = new TestReadMetricsQueue(LogLevel.OFF,true); StatementContext ctx = (StatementContext)Whitebox.getInternalState(rs, "context"); Whitebox.setInternalState(ctx, "readMetricsQueue", testMetricsQueue); Whitebox.setInternalState(rs, "readMetricsQueue", testMetricsQueue); }
@Test public void testAreAppNotificationsEnabled_callsPlatformMethod_whenOptOutProviderReturnsFalse() { mockAppLevelOptOutProvider(false); final NotificationClientBase notificationClient = Mockito.mock(NotificationClientBase.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); Whitebox.setInternalState(notificationClient, "pinpointContext", mockPinpointContext); notificationClient.areAppNotificationsEnabled(); verify(notificationClient).areAppNotificationsEnabledOnPlatform(); }
@Test public void testAreAppNotificationsEnabled_callsPlatformMethod_whenOptOutProviderReturnsFalse() { mockAppLevelOptOutProvider(false); final NotificationClientBase notificationClient = Mockito.mock(NotificationClientBase.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); Whitebox.setInternalState(notificationClient, "pinpointContext", mockPinpointContext); notificationClient.areAppNotificationsEnabled(); verify(notificationClient).areAppNotificationsEnabledOnPlatform(); }
@Test public void testAreAppNotificationsEnabled_callsPlatformMethod_whenOptOutProviderReturnsFalse() { mockAppLevelOptOutProvider(false); final NotificationClientBase notificationClient = Mockito.mock(NotificationClientBase.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); Whitebox.setInternalState(notificationClient, "pinpointContext", mockPinpointContext); notificationClient.areAppNotificationsEnabled(); verify(notificationClient).areAppNotificationsEnabledOnPlatform(); }
@Test public void testAreAppNotificationsEnabled_callsPlatformMethod_whenOptOutProviderReturnsFalse() { mockAppLevelOptOutProvider(false); final NotificationClientBase notificationClient = Mockito.mock(NotificationClientBase.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); Whitebox.setInternalState(notificationClient, "pinpointContext", mockPinpointContext); notificationClient.areAppNotificationsEnabled(); verify(notificationClient).areAppNotificationsEnabledOnPlatform(); }
@Test public void testFCMMessageReceivedInForegroundDefaultConfig() throws JSONException { // Force the app to be in the foreground. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(true); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails("12345")); 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("_campaign.received_foreground")); 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(4)); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("true")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); // Verify the notification is not posted and instead we get the result that the app was in the foreground. assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.APP_IN_FOREGROUND)); }
@Test public void testGCMMessageReceivedInForegroundDefaultConfig() throws JSONException { // Force the app to be in the foreground. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(true); NotificationClient.CampaignPushResult pushResult = target.handleGCMCampaignPush("12345", buildPushBundle(), Service.class); 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("_campaign.received_foreground")); 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(4)); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("true")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); // Verify the notification is not posted and instead we get the result that the app was in the foreground. assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.APP_IN_FOREGROUND)); }
@Test public void testFCMMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { // Force the app to be in the background. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails("12345")); 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("_campaign.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(5)); assertThat(receivedEvent.getAttribute("isOptedOut"), is("true")); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.OPTED_OUT)); }
@Test public void testBaiduMessageReceivedInForegroundDefaultConfig() throws JSONException { // Force the app to be in the foreground. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(true); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails()); 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("_campaign.received_foreground")); 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(4)); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("true")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); // Verify the notification is not posted and instead we get the result that the app was in the foreground. assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.APP_IN_FOREGROUND)); }
@Test public void testADMMessageReceivedInForegroundDefaultConfig() throws JSONException { // Force the app to be in the foreground. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(true); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails()); 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("_campaign.received_foreground")); 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(4)); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("true")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); // Verify the notification is not posted and instead we get the result that the app was in the foreground. assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.APP_IN_FOREGROUND)); }
@Test public void testGCMMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { // Force the app to be in the background final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationClient.CampaignPushResult pushResult = target.handleGCMCampaignPush("12345", buildPushBundle(), Service.class); 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("_campaign.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(5)); assertThat(receivedEvent.getAttribute("isOptedOut"), is("true")); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.OPTED_OUT)); }
@Test public void testADMMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { // Force the app to be in the background. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails()); 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("_campaign.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(5)); assertThat(receivedEvent.getAttribute("isOptedOut"), is("true")); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.OPTED_OUT)); }
@Test public void testBaiduMessageReceivedDefaultDoNotPostNotificationInForeground() throws JSONException { // Force the app to be in the background. final AppUtil appUtil = Mockito.mock(AppUtil.class); Whitebox.setInternalState(target.notificationClientBase, "appUtil", appUtil); Mockito.when(appUtil.isAppInForeground()).thenReturn(false); NotificationClient.CampaignPushResult pushResult = target.handleCampaignPush(buildNotificationDetails()); 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("_campaign.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(5)); assertThat(receivedEvent.getAttribute("isOptedOut"), is("true")); assertThat(receivedEvent.getAttribute("isAppInForeground"), is("false")); assertThat(receivedEvent.getAttribute("campaign_id"), is("Customers rule")); assertThat(receivedEvent.getAttribute("treatment_id"), is("Treat Me well please")); assertThat(receivedEvent.getAttribute("campaign_activity_id"), is("the brink of dawn")); // optOut is true because this test can't get the app icon resource id. assertThat(receivedEvent.getAllMetrics().size(), is(0)); assertTrue(pushResult.equals(NotificationClient.CampaignPushResult.OPTED_OUT)); }
@Before public void setup() { MockitoAnnotations.initMocks(this); final Context roboContext = RuntimeEnvironment.application .getApplicationContext(); spiedRoboContext = Mockito.spy(roboContext); mockPinpointContext = new AnalyticsContextBuilder() .withSystem(new MockSystem("JIMMY_CRACKED_CORN.and")) .withConfiguration(mockConfiguration) .withContext(spiedRoboContext) .build(); analyticsClient = new AnalyticsClient(mockPinpointContext); Whitebox.setInternalState(analyticsClient, "eventRecorder", mockEventRecorder); when(mockConfiguration.optBoolean("isAnalyticsEnabled", true)) .thenReturn(true); when(mockPinpointContext.getAnalyticsClient()) .thenReturn(analyticsClient); //target.setSessionClient(new SessionClient(mockContext, mockEventClient, mockTargetingClient, // new TestSessionStore(), false)); when(mockPinpointContext.getTargetingClient()) .thenReturn(mockTargetingClient); when(mockPinpointContext.getConfiguration()) .thenReturn(mockConfiguration); when(mockPinpointContext.getPinpointConfiguration()) .thenReturn(mockPinpointConfiguration); target = NotificationClient.createClient(mockPinpointContext, ChannelType.GCM); }
@Before public void setup() { MockitoAnnotations.initMocks(this); final Context roboContext = RuntimeEnvironment.application .getApplicationContext(); spiedRoboContext = Mockito.spy(roboContext); mockPinpointContext = new AnalyticsContextBuilder() .withSystem(new MockSystem("JIMMY_CRACKED_CORN.and")) .withConfiguration(mockConfiguration) .withContext(spiedRoboContext) .build(); analyticsClient = new AnalyticsClient(mockPinpointContext); Whitebox.setInternalState(analyticsClient, "eventRecorder", mockEventRecorder); when(mockConfiguration.optBoolean("isAnalyticsEnabled", true)) .thenReturn(true); when(mockPinpointContext.getAnalyticsClient()) .thenReturn(analyticsClient); //target.setSessionClient(new SessionClient(mockContext, mockEventClient, mockTargetingClient, // new TestSessionStore(), false)); when(mockPinpointContext.getTargetingClient()) .thenReturn(mockTargetingClient); when(mockPinpointContext.getConfiguration()) .thenReturn(mockConfiguration); when(mockPinpointContext.getPinpointConfiguration()) .thenReturn(mockPinpointConfiguration); target = NotificationClient.createClient(mockPinpointContext, ChannelType.ADM); }