CustomExceptionHandler implements Thread.UncaughtExceptionHandler { void storeException(Thread thread, Throwable ex) { String stackTrace = utils.trimToSize(getStackTraceString(ex), 1024); for (Service service : services) { if(service.isEnabled()){ exceptionPersister.store(new StoredException( service.getServiceType(), ex.getMessage(), thread.getName(), stackTrace )); } } } CustomExceptionHandler(Context context, List<CrashReportingService> crashReportingServices, boolean wifiOnly); @Override void uncaughtException(Thread thread, Throwable ex);  }
@Test public void testStoreException() { 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); when(mockService1.getServiceType()).thenReturn(ServiceType.HOCKEYAPP); when(mockService2.getServiceType()).thenReturn(ServiceType.PAGERDUTY); when(mockService3.getServiceType()).thenReturn(ServiceType.PAPERTRAIL); mockServices.add(mockService1); mockServices.add(mockService2); mockServices.add(mockService3); ExceptionPersister mockExceptionPersister = mock(ExceptionPersister.class); CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler(null, mockServices, false){ @Override String getStackTraceString(Throwable ex) { return "unit\ntesting\nstack\ntrace"; } }; customExceptionHandler.exceptionPersister = mockExceptionPersister; Thread thread = new Thread("unit_testing_thread"); Throwable throwable = new RuntimeException("unit testing exception"); customExceptionHandler.storeException(thread, throwable); ArgumentCaptor<StoredException> storedExceptionArgumentCaptor = ArgumentCaptor.forClass(StoredException.class); verify(mockExceptionPersister, times(2)).store(storedExceptionArgumentCaptor.capture()); List<StoredException> capturedStoredExceptions = storedExceptionArgumentCaptor.getAllValues(); assertNotNull(capturedStoredExceptions); assertEquals(2, capturedStoredExceptions.size()); assertEquals(ServiceType.HOCKEYAPP, capturedStoredExceptions.get(0).platform); assertEquals("unit testing exception", capturedStoredExceptions.get(0).message); assertEquals("unit\ntesting\nstack\ntrace", capturedStoredExceptions.get(0).stackTrace); assertEquals("unit_testing_thread", capturedStoredExceptions.get(0).threadName); assertEquals(ServiceType.PAPERTRAIL, capturedStoredExceptions.get(1).platform); assertEquals("unit testing exception", capturedStoredExceptions.get(1).message); assertEquals("unit\ntesting\nstack\ntrace", capturedStoredExceptions.get(1).stackTrace); assertEquals("unit_testing_thread", capturedStoredExceptions.get(1).threadName); }