CustomExceptionHandler implements Thread.UncaughtExceptionHandler { void sendStoredExceptions(){ if(!wifiOnly || utils.isOnWifi(context)) { Map<String, StoredException> storedExceptions = getStoredExceptions(); if(storedExceptions != null) { for (Map.Entry<String, StoredException> entry : storedExceptions.entrySet()) { String fileName = entry.getKey(); StoredException storedException = entry.getValue(); for (CrashReportingService service : services) { if (service.isEnabled()) { Callback<Object> callback = new DeleteFileCallback(context, fileName); service.logEvent(storedException, callback); } } } } } } CustomExceptionHandler(Context context, List<CrashReportingService> crashReportingServices, boolean wifiOnly); @Override void uncaughtException(Thread thread, Throwable ex);  }
@Test @SuppressWarnings("unchecked") public void testSendStoredExceptionsSimple() { List<CrashReportingService> mockServices = new ArrayList<>(); CrashReportingService mockService = mock(CrashReportingService.class); when(mockService.isEnabled()).thenReturn(true); mockServices.add(mockService); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(true); Context mockContext = mock(Context.class); final Map<String,StoredException> mockStoredExceptions = new LinkedHashMap<>(); StoredException storedException = new StoredException(); mockStoredExceptions.put("unit_testing.log", storedException); CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler(mockContext, mockServices, false){ @Override Map<String, StoredException> getStoredExceptions() { return mockStoredExceptions; } }; customExceptionHandler.utils = mockUtils; customExceptionHandler.sendStoredExceptions(); ArgumentCaptor<DeleteFileCallback> callbackArgumentCaptor = ArgumentCaptor.forClass(DeleteFileCallback.class); verify(mockService).logEvent(any(StoredException.class), callbackArgumentCaptor.capture()); assertEquals("unit_testing.log", callbackArgumentCaptor.getValue().storedExceptionFileName); assertSame(mockContext, callbackArgumentCaptor.getValue().context); }
@Test @SuppressWarnings("unchecked") public void testSendStoredExceptionsComplex() { List<CrashReportingService> mockServices = new ArrayList<>(); CrashReportingService mockService1 = mock(CrashReportingService.class); CrashReportingService mockService2 = mock(CrashReportingService.class); CrashReportingService mockService3 = mock(CrashReportingService.class); when(mockService1.isEnabled()).thenReturn(true); when(mockService2.isEnabled()).thenReturn(false); when(mockService3.isEnabled()).thenReturn(true); mockServices.add(mockService1); mockServices.add(mockService2); mockServices.add(mockService3); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(true); Context mockContext = mock(Context.class); final Map<String,StoredException> mockStoredExceptions = new LinkedHashMap<>(); StoredException storedException1 = new StoredException(); StoredException storedException2 = new StoredException(); mockStoredExceptions.put("unit_testing_1.log", storedException1); mockStoredExceptions.put("unit_testing_2.log", storedException2); CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler(mockContext, mockServices, false){ @Override Map<String, StoredException> getStoredExceptions() { return mockStoredExceptions; } }; customExceptionHandler.utils = mockUtils; customExceptionHandler.sendStoredExceptions(); ArgumentCaptor<DeleteFileCallback> callbackArgumentCaptor = ArgumentCaptor.forClass(DeleteFileCallback.class); verify(mockService1, times(2)).logEvent(any(StoredException.class), callbackArgumentCaptor.capture()); List<DeleteFileCallback> callbacksExecuted = callbackArgumentCaptor.getAllValues(); assertEquals("unit_testing_1.log", callbacksExecuted.get(0).storedExceptionFileName); assertEquals("unit_testing_2.log", callbacksExecuted.get(1).storedExceptionFileName); assertSame(mockContext, callbacksExecuted.get(0).context); assertSame(mockContext, callbacksExecuted.get(1).context); verify(mockService2, never()).logEvent(any(StoredException.class), any(Callback.class)); verify(mockService3, times(2)).logEvent(any(StoredException.class), callbackArgumentCaptor.capture()); callbacksExecuted = callbackArgumentCaptor.getAllValues(); assertEquals("unit_testing_1.log", callbacksExecuted.get(0).storedExceptionFileName); assertEquals("unit_testing_2.log", callbacksExecuted.get(1).storedExceptionFileName); assertSame(mockContext, callbacksExecuted.get(0).context); assertSame(mockContext, callbacksExecuted.get(1).context); }
@Test @SuppressWarnings("unchecked") public void testSendStoredExceptionsNoWifi() { List<CrashReportingService> mockServices = new ArrayList<>(); CrashReportingService mockService = mock(CrashReportingService.class); mockServices.add(mockService); Utils mockUtils = mock(Utils.class); when(mockUtils.isOnWifi(any(Context.class))).thenReturn(false); Context mockContext = mock(Context.class); final Map<String,StoredException> mockStoredExceptions = new LinkedHashMap<>(); mockStoredExceptions.put("unit_testing.log", new StoredException()); boolean wifiOnly = true; CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler(mockContext, mockServices, wifiOnly){ @Override Map<String, StoredException> getStoredExceptions() { return mockStoredExceptions; } }; customExceptionHandler.utils = mockUtils; customExceptionHandler.sendStoredExceptions(); verifyZeroInteractions(mockService); }